Sök…


Introduktion

I ASP.NET MVC placeras vyerna som standard i mappen Views . Ibland vill du ändra platserna och lagra vyerna någon annanstans.

Skapa en Visa Location Expander

För att kunna ändra visningsplatsen måste du implementera IViewLocationExpander . Metoden ExpandViewLocations returnerar en IEnumerable<string> innehåller de olika platserna att söka, med

public class MyViewLocationExpander : IViewLocationExpander
{
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        yield return "/CustomViewFolder/{1}/{0}.cshtml";
        yield return "/SharedFolder/{0}.cshtml";
    }

    public void PopulateValues(ViewLocationExpanderContext context)
    {            
    }
}

Registrera Visa Location Expander

Nu måste du registrera Expander för att den ska kunna användas av Razor View Engine. Lägg bara till detta i ConfigureServices i din Startup klass.

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<RazorViewEngineOptions>(options => {
        options.ViewLocationExpanders.Add(new MyViewLocationExpander());
    });
}


Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow