Open adriangodong opened 7 years ago
Hello. We have same problem with some views in a MVC project. Finally we move out views from this project, but we have another problems with this action. In a future these fake methods will be available?
Thanks @adriangodong. My Workaround was to create another class extending TemplateBase
public class MyRazorTemplate<T> : RazorEngine.Templating.TemplateBase<T>
{
protected void BeginContext(string virtualPath, int startPosition, int length, bool isLiteral) { }
protected void EndContext(string virtualPath, int startPosition, int length, bool isLiteral) { }
protected Fake Context { get; set; }
public class Fake
{
public object ApplicationInstance { get; set; }
}
}
RazorEngine.Templating.TemplateBase
andRazorEngine.Templating.TemplateBase<T>
are missingBeginContext
,EndContext
, andContext
members. These members are called fromaspnet_compiler
generated view classes.I propose we add empty/fake members that will resolve the compiler errors and make working with RazorEngine more seamless. I'm not sure how widespread or common this issue is, I welcome any feedback if there's a simpler workaround. I'm willing to work on a PR to resolve this if there's a consensus.
Rationale and Usage
Compiling views using
MvcBuildViews
msbuild task is a crucial step to avoid JIT view compilation errors. The task will runaspnet_compiler
on every build. The compiler does not have a way to whitelist / blacklist folders. This meansaspnet_compiler
will transform Razor templates into C# classes for all.cshtml
files in the project, including those inherited fromRazorEngine.Templating.TemplateBase
.Here's a sample of
aspnet_compiler
-generated C# class that fails build:Adding the members used by the generated class will allow RazorEngine-consumed Razor templates to sit side-by-side with ASP.NET MVC Razor templates and allow
MvcBuildViews
task to run without any issue.Proposed API
Add the following members to
TemplateBase
andTemplateBase<T>
:Workarounds
To avoid this issue without changing RazorEngine code, there are several workarounds:
Don't run
MvcBuildViews
/aspnet_compiler
. Not really an option unless you have a separate Razor templates validation tool.Create a build task that renames affected Razor templates so that
aspnet_compiler
don't include those files. There should be another build task that runs afterMvcBuildViews
that reverts the changes. (untested)Move Razor templates consumed by RazorEngine out of the ASP.NET MVC project so that
aspnet_compiler
does not generate C# classes for templates inheritingRazorEngine.Templating.TemplateBase
. Difficulty of moving will be affected by dependencies/complexity of the model classes used in the templates.