Working with IViewLocationExpander in mvc

2020-03-26 08:23发布

问题:

I want to render the view from custom location, so for that I have implemented IViewLocationExpander interface in a class. I have registered the same class in startup file as follows.

Startup.cs file

 public void ConfigureServices(IServiceCollection services)
    {
       .....
        //Render view from custom location.
        services.Configure<RazorViewEngineOptions>(options =>
        {
            options.ViewLocationExpanders.Add(new CustomViewLocationExpander());
        });
        ....
    }

CustomViewLocationExpander Class

public class CustomViewLocationExpander : IViewLocationExpander
{
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {

        var session = context.ActionContext.HttpContext.RequestServices.GetRequiredService<SessionServices>();
        string folderName = session.GetSession<string>("ApplicationType");

        viewLocations = viewLocations.Select(f => f.Replace("/Views/", "/" + folderName + "/"));


        return viewLocations;
    }

    public void PopulateValues(ViewLocationExpanderContext context)
    {

    }
}

and my application views structure is as follows

My issue is if I access the Views/Login view from ViewsFrontend folder from the url http://localhost:56739/trainee/Login/myclientname and immediately change the url in browser as http://localhost:56739/admin/Login/myclientname, then in this case it still refers to ViewsFrontend folder, which should refer to ViewsBackend folder.

Url with trainee should refer to ViewsFrontend folder and with admin should refer to ViewsBackend folder.

And after changing the url in browser it only calls PopulateValues method but not the ExpandViewLocations method.

So how can re-configure this class to work it for other folder ?

Thanks for the help !

回答1:

PopulateValues exists as a way to specify parameters that your view lookup would vary by on a per-request basis. Since you're not populating it, the view engine uses cached values from an earlier request. Add application type to PopulateValues and ExpandValues should get called:

public class CustomViewLocationExpander : IViewLocationExpander
{
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        string folderName = context.Values["ApplicationType"];
        viewLocations = viewLocations.Select(f => f.Replace("/Views/", "/" + folderName + "/"));

        return viewLocations;
    }

    public void PopulateValues(ViewLocationExpanderContext context)
    {
        var session = context.ActionContext.HttpContext.RequestServices.GetRequiredService<SessionServices>();
        string applicationType = session.GetSession<string>("ApplicationType");
        context.Values["ApplicationType"] = applicationType;
    }
}


回答2:

If you're trying to get Razor to locate views from another location, I've used in this technique in the past. ReSharper is also smart enough to pick up on that.