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
[[183871]] Heavy technology Preface In the proces...
Microsoft released its first cross-platform code ...
I heard that everyone has been tortured by data a...
Copyrighted images from the gallery, unauthorized...
When it comes to coffee production areas, you may...
Last week, the "dissolution of Jiyue Automob...
This year can be said to be the year of close com...
Recently, the Chinese Center for Disease Control ...
Recently, Great Wall Motors and BMW Holding (Neth...
Young people nowadays have developed the concept ...
[Daiyu Finance] Pantaoyuan Swallow Returns Strate...
A word of caution: The launch of Perfect Diary ha...
The systematized process standards were also ment...
Website optimization and ranking is a complex but...
Author: You Wenjuan, Editorial Director of World ...