I have the following layout for my mvc project:
Now in MVC 6 you can implement IViewLocationExpander
interface without messing around with view engines:
public class MyViewLocationExpander : IViewLocationExpander
{
public void PopulateValues(ViewLocationExpanderContext context) {}
public IEnumerable ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable viewLocations)
{
return new[]
{
"/AnotherPath/Views/{1}/{0}.cshtml",
"/AnotherPath/Views/Shared/{0}.cshtml"
}; // add `.Union(viewLocations)` to add default locations
}
}
where {0}
is target view name, {1}
- controller name and {2}
- area name.
You can return your own list of locations, merge it with default viewLocations
(.Union(viewLocations)
) or just change them (viewLocations.Select(path => "/AnotherPath" + path)
).
To register your custom view location expander in MVC, add next lines to ConfigureServices
method in Startup.cs
file:
public void ConfigureServices(IServiceCollection services)
{
services.Configure(options =>
{
options.ViewLocationExpanders.Add(new MyViewLocationExpander());
});
}