Interpretation of ASP. NET 5 & MVC6 series of tutorials (16): Custom View File Search logic, interpretation of ASP. NET
In MVC5 and earlier versions, if we want to control the path of the View File, we mustIViewEngine
InterfaceFindPartialView
OrFindView
Method, all view engines inherit from thisIViewEngine
Interface, such as the defaultRazorViewEngine
. However, in the new version of MVC6, the path to the view file is not the same. Currently, there are two ways:RazorViewEngine
, The other is through the new featuresIViewLocationExpander
Interface.
Use RazorViewEngine to control the View path
In the new versionRazorViewEngine
The class provides two virtual attributes (AreaViewLocationFormats
AndViewLocationFormats
), Which can be used to override the control without havingFindPartialView
OrFindView
Method, for example:
Public class ThemeViewEngine: RazorViewEngine {public ThemeViewEngine (extends pageFactory, IRazorViewFactory viewFactory, implements callback, IViewLocationCache viewLocationCache): base (pageFactory, viewFactory, callback, viewLocationCache) {} public override IEnumerable <string> AreaViewLocationFormats {get {var value = new Random (). Next (0, 1); var theme = value = 0? "Theme1": "Theme2"; // you can set another condition to return base for the skin type. areaViewLocationFormats. select (f => f. replace ("/Views/", "/Views/" + theme + "/"));}} public override IEnumerable <string> ViewLocationFormats {get {var value = new Random (). next (0, 1); var theme = value = 0? "Theme1": "Theme2"; // you can set another condition to return base for the skin type. viewLocationFormats. select (f => f. replace ("/Views/", "/Views/" + theme + "/"));}}}
Then, you can replace the view engine by modifying the ViewEngines instance attribute of MVcOptions. The Code is as follows:
services.AddMvc().Configure<MvcOptions>(options =>{ options.ViewEngines.Clear(); options.ViewEngines.Add(typeof(ThemeViewEngine));});
In this way, the system will follow the newly registeredThemeViewEngine
.
Use IViewLocationExpander to control the View path
In MVC6, Microsoft also provides another new method to control the path of the View File, that isIViewLocationExpander
Interface. You can implement the custom logic by implementing this interface, and you can also use the relevant context object. Example:
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 preceding customIViewLocationExpander
, Two methods are implemented:PopulateValues
AndExpandViewLocations
,PopulateValues
The method allows us to think aboutViewLocationExpanderContext
Add the response key-value pairs in the context for later use. Through this context object, we can findActionContext
AndHttpContext
Objects to use these objects for response judgment.ExpandViewLocations
This method is called only when there is no View cache or the View file of the corresponding key cannot be found in the View cache. In this method, the position of the View can be dynamically returned.
Finally, weStartup.cs
By modifyingRazorViewEngineOptions
Instance ObjectViewLocationExpanders
To achieve the purpose of registration, the Code is as follows:
services.Configure<RazorViewEngineOptions>(options =>{ options.ViewLocationExpanders.Add(typeof(ThemViewLocationExpander));});