Original: Interpreting ASP. 5 & MVC6 Series (16): Customizing View View File Lookup logic
Prior to MVC5 and previous versions, if we wanted to control the path to the view file, we had to override the IViewEngine
interface FindPartialView
or method, and FindView
all the view engines were inherited from the IViewEngine
interface, such as the default RazorViewEngine
. But in the new version of MVC6, the path to the view file is not the same, there are two ways, one is through RazorViewEngine
, the other is through the new feature IViewLocationExpander
interface.
Control the view path with Razorviewengine
In the new version RazorViewEngine
, the class provides two virtual attributes ( AreaViewLocationFormats
and ViewLocationFormats
) that can be used to override the control without having to FindPartialView
rewrite or FindView
override the method, as in the following example:
public class themeviewengine:razorviewengine{Public themeviewengine (irazorpagefactory pagefactory, IRazorView Factory viewfactory, Iviewlocationexpanderprovider viewlocationexpanderprovider, Iviewlocationcache ViewLoca Tioncache): Base (Pagefactory, Viewfactory, Viewlocationexpanderprovider, Viewlocationcache) {} public override ienumerable<string> Areaviewlocationformats {get {var value = new Random (). Next (0, 1); var theme = value = = 0? "Theme1": "Theme2"; Other conditions can be used to set the type of skin return base. Areaviewlocationformats.select (f = f.replace ("/views/", "/views/" + theme + "/")); }} public override ienumerable<string> Viewlocationformats {get {var value = NE W Random (). Next (0, 1); var theme = value = = 0? "Theme1": "Theme2"; Other conditions can be used to set the type of skin return base. Viewlocationformats.Select (f = f.replace ("/views/", "/views/" + theme + "/")); } }}
Then, you can replace the view engine by modifying the instance properties of Mvcoptions Viewengines the following code:
services.AddMvc().Configure<MvcOptions>(options =>{ options.ViewEngines.Clear(); options.ViewEngines.Add(typeof(ThemeViewEngine));});
This way, when the system looks for the view file, it executes according to the newly registered ThemeViewEngine
logic.
Control the view path with Iviewlocationexpander
In MVC6, Microsoft also provides a new way to control the path to the view file, which is the IViewLocationExpander
interface that implements the custom logic by implementing the interface, and can also use related context objects. Examples are as follows:
public class themeviewlocationexpander:iviewlocationexpander{public void Populatevalues ( Viewlocationexpandercontext context) {var value = new Random (). Next (0, 1); var theme = value = = 0? "Theme1": "Theme2"; Context. values["theme"] = theme; } public virtual ienumerable<string> expandviewlocations (Viewlocationexpandercontext context, Ienumerable<string> viewlocations) {return viewlocations.select (f = = F.replace ("/views/", "/views/" + context. values["Theme"] + "/")); }}
In the above customizations, the implementation of the IViewLocationExpander
2 methods are PopulateValues
and, the ExpandViewLocations
PopulateValues
method can let us want to ViewLocationExpanderContext
add a response to the context of the key value pair for subsequent use, we can take advantage of through the context object, to find ActionContext
and HttpContext
object, In order to make use of these objects to respond to the judgment operation, and the method ExpandViewLocations
, only if there is no view cache or the view cache can not find the corresponding key in the view file is called when the method, in which we can dynamically return the location of the view.
Finally, we Startup.cs
RazorViewEngineOptions
implement the registration purpose by modifying the properties of the instance object ViewLocationExpanders
, the code is as follows:
services.Configure<RazorViewEngineOptions>(options =>{ options.ViewLocationExpanders.Add(typeof(ThemViewLocationExpander));});
Synchronization and recommendations
This article has been synchronized to the Catalog index: Interpreting ASP. & MVC6 Series
Interpreting ASP 5 & MVC6 Series (16): Customizing View View File Lookup logic