You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在运行时从字符串解析Razor视图并处理动态绑定?

运行时解析并渲染数据库中的Razor模板内容

问题分析

你当前通过HtmlContentViewComponentResult返回数据库中的HTML内容时,Razor语法(如@Model.Name)会被当作纯文本输出——因为HtmlString仅标记内容为已编码HTML,并不会解析Razor指令。针对CMS场景,核心需求是实现运行时编译并执行Razor模板的能力。

简单实现方案:使用RazorLight库

RazorLight是轻量级Razor模板引擎,原生支持运行时编译,非常适合从字符串渲染Razor内容。

步骤1:安装NuGet包

Install-Package RazorLight

步骤2:在ViewComponent中集成RazorLight

修改InvokeAsync方法,当加载到原始Razor模板内容时,通过RazorLight编译并渲染:

private readonly IRazorLightEngine _razorEngine;
private readonly IComponentProvider _componentProvider;

// 构造函数注入依赖
public YourViewComponent(IComponentProvider componentProvider, IRazorLightEngine razorEngine)
{
    _componentProvider = componentProvider;
    _razorEngine = razorEngine;
}

public async Task<IViewComponentResult> InvokeAsync(int Id)
{
    var component = await _componentProvider.GetComponentByIdAsync(Id);

    if (component != null && component.Configurations.Any())
    {
        string viewPath = component.GetConfiguration<string>(VIEW_PATH);
        if (viewPath != null)
        {
            return View(viewPath);
        }

        string content = component.GetConfiguration<string>(VIEW_RAW);
        if (!string.IsNullOrEmpty(content))
        {
            // 替换为你的ViewComponent实际使用的模型实例
            var model = new YourComponentModel(); 
            // 编译并渲染模板,用Component_{Id}作为模板唯一标识,避免缓存冲突
            string renderedHtml = await _razorEngine.CompileRenderStringAsync($"Component_{Id}", content, model);
            return new HtmlContentViewComponentResult(new HtmlString(renderedHtml));
        }
    }

    return Content(string.Empty);
}

步骤3:配置RazorLight依赖注入

在Program.cs中注册RazorLight服务:

builder.Services.AddRazorLight(options =>
{
    // 启用模板缓存,避免重复编译相同内容(推荐配置)
    options.CachingProvider = new DefaultCachingProvider();
});

注意事项

  • 安全风险:允许用户编辑Razor模板存在代码注入风险,需严格校验输入,禁止执行危险操作(如文件读写、反射调用),必要时添加沙箱限制。
  • 性能优化:利用RazorLight的缓存机制,对编译后的模板进行复用,减少重复编译开销。

内容的提问来源于stack exchange,提问作者Mark Redfern

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 04:07:08