c# – Razor ViewEngine临时编译.cs文件
发布时间:2020-12-16 01:50:32 所属栏目:百科 来源:网络整理
导读:在Razor ViewEngine中调用Parse方法时,编译错误将作为TemplateComplilationException抛出,其中包含错误列表.这些错误指的是临时文件名,但文件会在您访问之前被删除. static void Main(string[] args) { var service = TemplateServiceFactory.CreateTemplate
在Razor ViewEngine中调用Parse方法时,编译错误将作为TemplateComplilationException抛出,其中包含错误列表.这些错误指的是临时文件名,但文件会在您访问之前被删除.
static void Main(string[] args) { var service = TemplateServiceFactory.CreateTemplateService(Language.CSharp,true); string result = ""; try { result = service.Parse("Hello @DateTime.NowXX "); } catch (TemplateCompilationException ex) { foreach (var error in ex.Errors) if (!string.IsNullOrEmpty(error.FileName)) Console.WriteLine( File.ReadAllText( error.FileName )); } // ^^^^ File does not exist! Console.WriteLine( result ); Console.ReadKey(); } (一点背景) 解决方法
RazorEngine的TemplateCompilationException是一个包含一个包含
CompilerError个对象的CompilerErrorCollection的类,因此您可能从TemplateCompilationException CompilerError对象获得的大部分细节都是它们各自的属性,这似乎足以进行调试.考虑并尝试这个例子
try { Razor.Parse("My erroneous @DateTime.Now.foo()"); } catch(TemplateCompilationException ex) { foreach(var error in ex.Errors) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Compile Error Num: t" + error.ErrorNumber); sb.AppendLine("Error Text:nt" + error.ErrorText); Console.WriteLine(sb.ToString()); } Console.WriteLine("Erroneous Template:nt" + ex.Template); } 当我运行我的示例时,这就是我得到的,它告诉您遇到的错误,您可以转储模板数据以供用户参考. Compile Error Num: CS1061 Error Text: 'System.DateTime' does not contain a definition for 'foo' and no extension method 'foo' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?) Erroneous Template: My erroneous @DateTime.Now.foo() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |