How can I render a razor view to a string without

2019-03-30 10:14发布

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.

1条回答
一夜七次
2楼-- · 2019-03-30 11:13

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" });
查看更多
登录 后发表回答