能否在WebForms Web应用程序中使用Razor引擎(razorengine.codeplex.com)?我正在使用文件来定义模板,例如:
Sample.cshtml
<h1>Hello</h1>
或者sample2.cshtml(使用继承启用智能感知)
@inherits SomeBaseClass<SomeModelClass>
<h1>Hello @Model.SomeProperty</h1>
在应用程序启动时,我编译模板:
//get files and loop
RazorEngine.Razor.Compile(templateString, razorPath);
在用户控件的代码后台,我加载/解析模板:
RazorEngine.Razor.Run<SomeModelClass>(model, razorPath);
问题:
Sample.cshtml 抛出以下异常:
类型“ASP._Page_razor_Sample_cshtml”不继承自“System.Web.UI.UserControl”。
Sample2.cshtml 抛出以下异常(异常代码如下,从编译后的 cshtml 源代码中提取...):
CS0103: 当前上下文中不存在名称为“Context”的内容
Line 37: protected ASP.global_asax ApplicationInstance {
Line 38: get {
Line 39: return ((ASP.global_asax)(Context.ApplicationInstance));
Line 40: }
Line 41: }
在这种情况下使用Razor引擎是否可行?