Develop standard template Razor - ASPNET MVC

3

I have an ASPNET MVC5 project using C # which, by design definition it was agreed that the layout of the page structure would be stored in the database.

Within my _ViewStart would be referenced my _Layouts.cshtml that would be the common content of the pages. So the problem is that the content that would be the structure of the site would be fetched from the database, with all HTML and Razor elements, and rendered on the screen. For simple HTML (without any Razor) this would work quietly with some of the following alternatives (code from view _layouts.cshtml where ContentHTML 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 (in addition, in my View the compiler does not run without RenderBody () explicit there, even though I have already inserted it next to the bank record).

I searched here and saw a possible solution using RazorEngine, however in my tests here it is not working in any way for more complex HTML's (involving Custom Helpers for example).

Could anyone help me with some feature idea to implement?

Example of 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 not controler

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

    
asked by anonymous 05.02.2015 / 13:09

1 answer

1

I was able to resolve using the superscript VirtualPathProvider.

TobyMosque's contribution was helpful through the link link.

    
06.02.2015 / 18:53