Parse Razor string in ASP.NET core
Asked Answered
S

1

10

In ASP.NET MVC I could parse Razor scripts like this:

string template = "It is @DateTime.Now.ToString()";
string result = Razor.Parse(template);

But this static doesn't exist in core.

Also I've read this post, but in my case the view code is a string, so I can't use the find or get view methods from the ICompositeViewEngine.

Any advice on how to get a view string parsed in core?

Shattuck answered 29/6, 2016 at 12:22 Comment(2)
Possible duplicate of Render Razor view to string in ASP.NET 5Cementum
Possible duplicate of Render Razor View to string in ASP.NET CoreGastongastralgia
C
5

I've already answered the question Here

Today I've finished with my library that can solve your problem. You can use it out of ASP.NET as it has no dependencies on it

This is how you can use it

string template = "It is @DateTime.Now.ToString()";
string result = new LightRazorEngine().ParseString(template);

More: https://github.com/toddams/RazorLight

Cementum answered 24/7, 2016 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.