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
Technology is advancing at a rapid pace, which ra...
As WeChat and Weibo operations become increasingl...
As the largest knowledge sharing platform in Chin...
Author: Zhou Paopi You think this is an article a...
Douyin and Kuaishou are both platforms where we h...
The automotive industry is undercurrent, with pri...
Author: Gong Weiyan, Associate Researcher, Instit...
Recently, although the temperature has repeatedly...
This year, the "violent plum" season ca...
The day before yesterday afternoon, the cross-bor...
The landing page may seem like just a few simple ...
Last week, a friend on Zhihu asked Su Yan a quest...
Which financial management training course is bet...
In today's IT world, the brightest lights com...