Working with IViewLocationExpander in mvc

偶尔善良 提交于 2019-12-01 07:09:11

问题


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.



来源:https://stackoverflow.com/questions/39471627/working-with-iviewlocationexpander-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!