.NETCore动态解析Razor代码cshtml代码解析RazorEngine.NetCore

需求
在项目开发过程中中有个需求,需要实现动态页面内容,希望是前台用户可以编写Razor代码片段,然后后台可以把代码片段渲染成最终的html块,放置到界面中
就比如.net core mvc中的cshtml渲染那样,发布后也可以修改cshtml文件,然后浏览器F5就能看到最终效果
实现:
01、项目中添加 RazorEngine.NetCore 包

02、实现代码
添加using引用
生成代码
C# 全选
using Microsoft.AspNetCore.Mvc;
using RazorEngine;
using RazorEngine.Templating;
namespace YESCMS.Controllers
{
    public class TestRazorController : ControllerBase
    {
        public IActionResult test()
        {
            var template = "Hello @Model.Name, welcome to use RazorEngine!";
            var result = Engine.Razor.RunCompile(template, "templateKey1", null, new { Name = "World" });
            return Content(result);
        }
    }
}运行测试

官方文档
github地址:https://github.com/Antaris/RazorEngine
文档地址:RazorEngine (antaris.github.io)
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
post 管理员  
 
 

