在 ASP.NET 核心中解析 Razor 字符串

Posted

技术标签:

【中文标题】在 ASP.NET 核心中解析 Razor 字符串【英文标题】:Parse Razor string in ASP.NET core 【发布时间】:2016-06-29 12:22:37 【问题描述】:

在 ASP.NET MVC 中,我可以像这样解析 Razor 脚本:

string template = "It is @DateTime.Now.ToString()";
string result = Razor.Parse(template);

但核心中不存在这种静态。

我也读过这个post,但在我的情况下,视图代码是一个字符串,所以我不能使用ICompositeViewEngine 中的findget 视图方法。

关于如何在核心中解析视图字符串有什么建议吗?

【问题讨论】:

Render Razor view to string in ASP.NET 5的可能重复 Render Razor View to string in ASP.NET Core的可能重复 【参考方案1】:

我已经回答了Here的问题

今天我已经完成了可以解决您问题的库。您可以在 ASP.NET 之外使用它,因为它没有 对它的依赖

你可以这样使用它

string template = "It is @DateTime.Now.ToString()";
string result = new LightRazorEngine().ParseString(template);

更多:https://github.com/toddams/RazorLight

【讨论】:

以上是关于在 ASP.NET 核心中解析 Razor 字符串的主要内容,如果未能解决你的问题,请参考以下文章