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
As we enter 2022, the epidemic, which had been do...
This year's Double 11 once again made domesti...
The quality of the recorded video cannot be compa...
A friend of Huazi has been having headaches recen...
Grandpa Zhang, who has been raising cattle and sh...
What is the largest hydroelectric project in the ...
From November 28 to 30, 2020, the 2020 Changsha C...
Everyone chases after heavy users, thinking they ...
=========================================...
In daily life, odorous shoes are a headache. When...
As the Matthew effect in the industry deepens, it...
This question is actually very simple. Three-dime...
Ford Edge: Beats Highlander! Volkswagen Touron: B...