I have a project ASPNET MVC5 using C# by project definition is gone agreed that the structure of the page layout would be stored in the database.
Inside my _ViewStart would be referenced by _Layouts.cshtml would be the common content of the page. So far so good, the problem is that the content would be the site structure would be fetched from the database, with all HTML and Razor elements, and rendered on the screen. In simple HTML (when don´t have Razor) this would work fine with some of the following alternatives (the view _layouts.cshtml code where ConteudoHTML would be sought from the bank and returned by the controller):
@ViewBag.ConteudoHTMLor@Html.Raw(ViewBag.ConteudoHTML.ToString())- Create some Helper and call in the view:
@MeuHelper.Template(ViewBag.ConteudoHTML.ToString())
The problem is that I store in the database structure already contains references to my Models, ViewBag, Helpers, etc. When rendering the browser does not recognize Razor elements (besides, in my view the compiler does not perform without RenderBody() explicit there, even though I have already inserted it next to the database record).
Could anyone help me?
HTML code in the database
<!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>
Controller code:
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();
}
View code (_Layouts.cshtml)
@ViewBag.ConteudoHTML