1

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.ConteudoHTML or @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
1

3 Answers 3

1

See this question for a method of parsing Razor views into HTML. In short, you use the view engine to find your view, created a context for it, and use that context to render the view so you can get the HTML results.

Why did you decide to use HTML stored in the database? That sounds like a very poor path to go down, it is going to make your pages take longer to load and be very difficult to maintain.

You really should just use a layout page for your common code, this will make things much simpler.

Sign up to request clarification or add additional context in comments.

2 Comments

I want users to be able to customize Views, but without them having access to the Web solution or source code.
I looked at your posting to VirtualPathProvider, and while I have not tested it, I would be worried about security. I do not know what types of customization you wish to give to your users, but if there is any opportunity for them to provide code or input to the view, then there is a huge risk of SQL injection you must be prepared to deal with. Maybe a Javascript templating library would be more in line with what you need? You could dynamically create JavaScript if you needed to for complex view customization, for simple situations you could store customizations as JSON.
0

Razor views are compiled before they're used. You're trying to inject a string into the view as code, but that doesn't happen until after the view has compiled. You would need a way to dynamically create a razor view in order to do what you want to do. Having said that, storing your markup in the database is a major code smell.

1 Comment

Yes, I will test VirtualPathProvider (umbraworks.net/bl0g/rebuildall/2009/11/17/…) if work fine I will post as a solution.
0

I solve my question using VirtualPathProvider.

Reference: http://www.umbraworks.net/bl0g/rebuildall/2009/11/17/ASP_NET_MVC_and_virtual_views

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.