View a view from a database in MVC 6

We are working on an ASP.NET MVC 6 project, and you need to render Razor views different from the file system source (in particular, Azure Blob storage, but that doesn't matter). Previously (in MVC 5), you could create and register your own VirtualPathProvider, which can view content from DB or resource DLLs (for example).

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

UPD: Here is an example of the 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 arises: how to implement this GetViewFromString function?

+6
source share
2 answers

You need to configure 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 LanguageViewLocationExpander: https://github.com/aspnet/Mvc/blob/ad8ab4b8fdb27494f5dece6f1186acea03f9dd52/test/WebSites/RazorWebSite/Services/LanguageViewLocationExpander.cs

Based on the AzureBlobLocationExpander , you should put you on the right track.

+2
source

Just posted a sample .cshtml repository in Azure Blob repository for GitHub .

See also my answer to another question on this

Basically, you need to create an implementation of IFileProvider. This can then be registered in Startup.cs by setting up RazorViewEngineOptions

 services.Configure<RazorViewEngineOptions>(options => { options.FileProvider = new AzureFileProvider(Configuration); }); 
-1
source

Source: https://habr.com/ru/post/984817/


All Articles