2
votes

I have a asp.net core 1.1 project, and I need to get the compiled C# code of a cshtml view that contains tag helpers.

I am aware of the possibility to precompile the razor views, but this does not help since it generates a dll containing the IL code corresponding to the view and not the C# code.

I found the following article that explains how to use the razor engine outside of asp.net mvc, but it is not relevant for asp.net core.

1
Why would you want the c# code and not the compiled DLL. And why the c# code and not the razor?Joel Harkes

1 Answers

2
votes

I'm an author of RazorLight library (https://github.com/toddams/RazorLight), an alternative to RazorEngine that can run on .NET Core.

It can partly solve your problem. Why partly, because at the moment tag helpers are not supported, but it is planned to implement them in the future. Take a look at it, I believe it might be useful for you. Feel free to ask any questions

Update:

var engine = EngineFactory.CreatePhysical("path here");

string content = "your content to be compiled";
var modelInfo = new ModelTypeInfo(typeof(YourViewModel));

string razorTemplate = engine.Core.GenerateRazorTemplate(new LoadedTemplateSource(content), modelInfo);