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
How can we avoid loss of users that we have worke...
According to The Verge, Google will make a major ...
You wake up from your sleep and find that everyth...
As a creative planner, a core professional qualit...
Recently, Tesla has begun to push the 8.0 version...
In March 1986, four scientists, Wang Daheng, Wang...
I have been constantly changing devices from Noki...
Why are short videos so popular? What is the real...
I believe most people know that the profits of jo...
After opening the Xigua video dissemination task ...
On June 29, 2021, the "July 1st Medal" ...
The Tibetan fox , with its confused and world-wea...
If we move the time back to five or even ten year...
According to foreign media reports, after Alibaba...
Daily Specials are the hope of many small sellers...