문제

Is it possible to use the Razor engine (razorengine.codeplex.com) in a webforms webapplication? I'm using files to define the templates e.g.

Sample.cshtml

<h1>Hello</h1>

Or sample2.cshtml (with inherits to enable intellisense)

@inherits SomeBaseClass<SomeModelClass>
<h1>Hello @Model.SomeProperty</h1>

On application start i compile the templates:

//get files and loop
RazorEngine.Razor.Compile(templateString, razorPath);

In a user control code-behind I load / parse the template:

RazorEngine.Razor.Run<SomeModelClass>(model, razorPath);

Problem:

Sample.cshtml gives the following exception:

Type 'ASP._Page_razor_Sample_cshtml' does not inherit from 'System.Web.UI.UserControl'.

Sample2.cshtml gives the following exception (code where exception occurs is below, extracted from compiled cshtml source...):

CS0103: The name 'Context' does not exist in the current context

Line 37:         protected ASP.global_asax ApplicationInstance {
Line 38:             get {
Line 39:                 return ((ASP.global_asax)(Context.ApplicationInstance));
Line 40:             }
Line 41:         }

Is it even possible to use the Razor engine in this fashion?

도움이 되었습니까?

해결책 2

This problem was not related to the Razor engine in any way. I was using a parameter somehwere in my CMS which happened to be a reserved word (undocumented!). Using path as a parameter name where it is also used by the system itself can cause some pretty strange results... Even more so if the parameter value is indeed a path that leads to a file...

다른 팁

I found some info on using Razor with Master Pages / Partials:

http://www.eworldui.net/blog/post/2011/01/07/Using-Razor-Pages-with-WebForms-Master-Pages.aspx

Though I don't think you can use it generally within webforms.

However, ScottHa put a project on NuGet that made it easier to enable MVC inside a WebForms app, so this might be useful if you're just trying to use it for specific things within your app. He demo'd this in his NuGet talk with Phil Haack at Mix:

http://nuget.org/List/Packages/AddMvc3ToWebForms

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top