2

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? Commented Feb 25, 2017 at 18:39

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

can you please give me an example of how to get the C# code and not the final generated content(which is typically an html string) :-)?
I will update my answer later with a code example, as I'm with my phone at the moment, which is not very handy)
@Toddams Can you please provide an example for current version of RazorLight?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.