Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render view from DB in MVC 6

We are working on ASP.NET MVC 6 project and it's necessary to render Razor views from other than file system source (Azure Blob storage in particular but it's not important). Earlier (in MVC 5) it was possible to create and register custom VirtualPathProvider which can take view content from DB or resource DLLs (for example).

It seems that approach has been changed in MVC 6. Does anybody know where to look for?

UPD: Here is an example of code I'm looking for:

   public IActionResult Index()
    {

        ViewBag.Test = "Hello world!!!";
        string htmlContent = "<html><head><title>Test page</title><body>@ViewBag.Test</body></html>";

        return GetViewFromString(htmlContent);
    }

The question is: how to implement that GetViewFromString function?

like image 483
Sergiy Avatar asked Apr 07 '15 20:04

Sergiy


People also ask

What is rendered in view of MVC?

Rendering UI with ViewsA view renders the appropriate UI by using the data that is passed to it from the controller. This data is passed to a view from a controller action method by using the View method. The Views folder is the recommended location for views in the MVC Web project structure.

What is _layout Cshtml in MVC?

The file "_Layout. cshtml" represents the layout of each page in the application. Right-click on the Shared folder in Solution Explorer then go to "Add" item and click on "View". Now the View has been created.


1 Answers

You need to configure a ViewLocationExpander:

services.SetupOptions<RazorViewEngineOptions>(options =>
{
    var expander = new LanguageViewLocationExpander(
        context => context.HttpContext.Request.Query["language"]);
    options.ViewLocationExpanders.Insert(0, expander);
});

and here is the implementation for the LanguageViewLocationExpander : https://github.com/aspnet/Mvc/blob/ad8ab4b8fdb27494f5dece6f1186acea03f9dd52/test/WebSites/RazorWebSite/Services/LanguageViewLocationExpander.cs

Basing your AzureBlobLocationExpander on that one should put you in the right track.

like image 156
Bart Calixto Avatar answered Oct 01 '22 13:10

Bart Calixto