5

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.

jaffa
  • 26,770
  • 50
  • 178
  • 289
  • 1
    You may take a look at the [following blog post](http://buildstarted.com/2010/09/29/razor-view-engine-without-mvc-at-all/). And [another one](http://buildstarted.com/2010/09/28/mvc-3-razor-view-engine-without-a-controller/). – Darin Dimitrov Mar 09 '11 at 10:48

1 Answers1

9

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" });
cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
Daniel Liuzzi
  • 16,807
  • 8
  • 52
  • 57