【发布时间】:2015-02-07 02:02:12
【问题描述】:
我有一个项目 ASPNET MVC5 使用 C# 通过项目定义已经消失,同意将页面布局的结构存储在数据库中。
_Layouts.cshtml 将引用我的 _ViewStart 内部,这将是页面的常见内容。到目前为止一切顺利,问题在于内容将是从数据库中获取站点结构,包含所有 HTML 和 Razor 元素,并呈现在屏幕上。在简单的 HTML 中(当没有 Razor 时),这适用于以下一些替代方案(视图 _layouts.cshtml 代码,其中 ConteudoHTML 将从银行中查找并由控制器返回):
-
@ViewBag.ConteudoHTML或@Html.Raw(ViewBag.ConteudoHTML.ToString()) - 创建一些 Helper 并在视图中调用:
@MeuHelper.Template(ViewBag.ConteudoHTML.ToString())
问题是我存储在数据库结构中已经包含对我的模型、ViewBag、Helper 等的引用。渲染时浏览器无法识别 Razor 元素(此外,在我看来,如果没有RenderBody(),编译器将无法执行明确的,即使我已经将它插入到数据库记录旁边)。
谁能帮帮我?
数据库中的 HTML 代码
<!DOCTYPE html>
<html lang="pt-br" xmlns="http://www.w3.org/1999/xhtml" xml:lang="pt-br">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="@ViewBag.MetaDescription" />
<meta name="keywords" content="@ViewBag.MetaKeywords" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>@ViewBag.Title</title>
<!--[if IE]><link rel="shortcut icon" href="~/css/images/favicon.ico"><![endif]-->
<link rel="icon" href="@Url.Content("~/css/images/favicon.png")" />
<link rel="stylesheet" type="text/css" href="@Url.Content("~/css/bootstrap.min.css")" />
@RenderSection("Css", required: false)
</head>
<body>
@RenderBody()
</body>
</html>
控制器代码:
public virtual ActionResult Index()
{
ViewBag.ConteudoHTML = "";//HTML vindo do banco, html acima
ViewBag.Title = "Meu título";
ViewBag.MetaDescription = "Description";
ViewBag.MetaKeywords = "Keywords";
return View();
}
查看代码 (_Layouts.cshtml)
@ViewBag.ConteudoHTML
【问题讨论】:
标签: c# asp.net-mvc razor