Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I render a razor view to a string without dependency on a controller context?

How can I render a razor view to a string without dependency on a controller context?

All the examples I have seen involve passing in a ControllerContext to my render helper class. However, I don't want the dependency to the controller context as I want the rendered string to be generated inside a service.

like image 878
jaffa Avatar asked Mar 09 '11 10:03

jaffa


1 Answers

RazorEngine seems to do exactly what you're looking for:

string template = "Hello @Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });
like image 171
Daniel Liuzzi Avatar answered Oct 07 '22 00:10

Daniel Liuzzi