In MVC5 and previous versions, if we want to control the path of the View file, we must rewrite the FindPartialView or FindView method of the IViewEngine interface. All view engines inherit from the IViewEngine interface, such as the default RazorViewEngine. But in the new version MVC6, the path of the view file is different. There are currently two ways, one is through RazorViewEngine, and the other is through the new feature IViewLocationExpander interface. In the new version of RazorViewEngine, this class provides two virtual properties (AreaViewLocationFormats and ViewLocationFormats) that can be used to rewrite the control without having to rewrite the FindPartialView or FindView method. The example is as follows:
In this way, when the system searches for view files, it will execute according to the logic of the newly registered ThemeViewEngine. In MVC6, Microsoft also provides another new way to control the path of the View file, that is, the IViewLocationExpander interface. By implementing this interface, you can implement custom logic and use related context objects. The example is as follows:
In the above custom IViewLocationExpander, two methods are implemented, namely PopulateValues and ExpandViewLocations. The PopulateValues method allows us to add corresponding key-value pairs to the ViewLocationExpanderContext context for subsequent use. Through this context object, we can use it to find ActionContext and HttpContext objects, so as to use these objects to make corresponding judgment operations; and the ExpandViewLocations method will only be called when there is no View cache or the View file with the corresponding key cannot be found in the View cache. In this method, we can dynamically return the location of the view. ***, we achieve the registration purpose by modifying the ViewLocationExpanders property of the RazorViewEngineOptions instance object in Startup.cs. The code is as follows:
|
<<: Interpreting ASP.NET 5 & MVC6 Series (15): MvcOptions Configuration
>>: Interpreting ASP.NET 5 & MVC6 Series (17): Other new features in MVC
Will the three-year Spring Festival red envelope ...
Source code introduction: MySpringMenu is a cool ...
In recent years, lipid-lowering injections have a...
This article takes the mainstream payment system ...
APP promotion online promotion is important, but ...
On the morning of August 21, the 2022 Future Scie...
Acute attack of primary acute angle-closure glauc...
Reviewer of this article: Chen Haixu, Deputy Dire...
[[120832]] As one of the greatest and most import...
The world of questions has completely come alive....
When you think of "absorbing carbon dioxide&...
Two weeks ago, Apple previewed iOS 16 to the outs...
According to foreign media reports, headhunting f...
At present, many studies have shown that eating b...
1. Industry dynamics summary Industry Development...