Working with IViewLocationExpander in mvc #5279
Comments
/cc @pranavkm |
Caching includes the Move the |
Copying my answer from SO:
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;
}
} |
@dougbu thanks for your comment. That was somehow expanded in a SO answer. The net result is that this point should be made more clear in official documentation. For example, current sentence found in code comments is not totally clear:
Not sure how/to who submit an issue/PR for this. EDIT I now see that description on actual doc page is more comprehensive:
|
@BrainCrumbz this is a closed issue. If you think the |
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
CustomViewLocationExpander Class
and my application views structure is as follows
http://i.stack.imgur.com/HyOhW.png
My issue is if I access the Views/Login view from
ViewsFrontend
folder from the urlhttp://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 and renders views from
ViewsFrontend
folder, which is a previous folder and it should refer toViewsBackend
folder.Url with
trainee
should refer toViewsFrontend
folder and withadmin
should refer toViewsBackend
folder.And after changing the url in browser it only calls
PopulateValues
method but not theExpandViewLocations
method. My view names are same in both of the folders. I know this is happening because of caching, it seems it only caching the view name and not the entire path of the view.So how can re-configure this class OR make
ViewLocationExpanderContext
null to work it for other folder ?Thanks for the help !
The text was updated successfully, but these errors were encountered: