How to render a Razor View to a string in ASP.NET MVC 3?

You can achieve that with the razorengine.

string template = "Hello @Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });

And it does not rely on the controller context – but because of that you are not able to use the Html helpers (which rely on the http context). But it is perfect to use razor as a template engine for strings.

Leave a Comment