如何在不依赖控制器上下文的情况下将剃刀视图呈现为字符串? [英] 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?
我所看到的所有示例都涉及将ControllerContext传递给渲染助手类.但是,我不希望依赖于控制器上下文,因为我希望在服务内部生成呈现的字符串.
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.
推荐答案
RazorEngine 似乎确实在做什么您正在寻找:
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" });
这篇关于如何在不依赖控制器上下文的情况下将剃刀视图呈现为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文