从渲染数据库或其他来源的剃刀(Render razor from database or other

2019-10-18 23:12发布

I would like to render the razor code from the database to to a razor view. Would thatbe possible?

CONTROLLER/ACTION:

public ActionResult About()
{
    ViewBag.Message = "Your application description page.";
    ViewBag.RazorCode = "@Html.TextBox(\"txtTestRazor\")";
    return View();
}

VIEW:

@{
    ViewBag.Title = "About";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<p>Use this area to provide additional information.</p>

<h3>@ViewBag.RazorCode</h3>

HTML/OUTPUT:

About.

Your application description page.

Use this area to provide additional information.
@Html.TextBox("txtTestRazor")

Answer 1:

一种选择是使用RazorEngine

基础知识是很容易的,只要给它一个字符串(和模型)

string template = "Hello @Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });

它不支持HTML辅助,路由等开箱即用(据我记得)。



文章来源: Render razor from database or other sources