3
I am with an ASPNET MVC5 project using C# which by design definition was agreed that the Layout of the pages structure would be stored in the Database.
Within my _Viewstart would be referenced my _Layouts.cshtml which would be the common content of the pages. So far so good, the problem is that the content that would be the structure of the site would be searched from the database, with all the elements HTML and Razor, and rendered on the screen. For simple HTML (without any Razor) this would work smoothly with some of the following alternatives (view code _layouts.cshtml where Contentohtml would be fetched from the database and returned by the controller):
- @Viewbag.Conteudohtml or @Html.Raw(Viewbag.ConteudoHTML.Tostring())
- or even creating a Helper and referencing in the view: @Meuhelper.Template(Viewbag.ConteudoHTML.Tostring())
The problem is that I store in the database the structure that already contains my references to Models, Viewbag, Helpers, etc. When rendering the browser does not recognize the elements of Razor (moreover, in my View the compiler does not run without Renderbody() explicitly there, even if I have already entered it next to the bank register).
I searched here and saw a possible solution using Razorengine, but in my tests here it is not working in any way for more complex HTML’s (involving Custom Helpers for example).
Could someone help me with some resource idea to implement?
Example of the contents: - HTML stored 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>
-- Code on the controller
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
OBS: as I tried to implement this in several ways there is no way to put all solutions I tried, but do not diverge much of this.
If Scottgu can load the Views from anywhere. But it’s a little more complicated than just playing a String in View(*.cshtml) To do so, you need to register a Virtualpathprovider. Below is a good tutorial on how to do it: http://www.umbraworks.net/bl0g/rebuildall/2009/11/17/ASP_NET_MVC_and_virtual_views
– Tobias Mesquita
I tried to work by overwriting this Virtualpathprovider, but when doing a proof of concept it did not work as expected. I’m thinking it’s the version of ASPNET MVC. Anyway the tutorials I followed are different from that there, I’ll try this and put the result.
– Augusto Filipe
From what I understand the file is not on disk: it is on a bench. The design of Lenard Gunda partially solves your problem.
– Leonel Sanches da Silva