Interpreting ASP.NET 5 & MVC6 Series (16): Customizing View View File Search Logic

Interpreting ASP.NET 5 & MVC6 Series (16): Customizing View View File Search Logic

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.
Controlling View Paths with RazorViewEngine

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:

  1. public   class ThemeViewEngine : RazorViewEngine
  2. {
  3. public ThemeViewEngine(IRazorPageFactory pageFactory,
  4. IRazorViewFactory viewFactory,
  5. IViewLocationExpanderProvider viewLocationExpanderProvider,
  6. IViewLocationCache viewLocationCache)
  7. : base(pageFactory,
  8. viewFactory,
  9. viewLocationExpanderProvider,
  10. viewLocationCache)
  11. {
  12. }
  13.  
  14. public override IEnumerable<string> AreaViewLocationFormats
  15. {
  16. get
  17. {
  18. var value = new Random().Next( 0 , 1 );
  19. var theme = value == 0 ? "Theme1" : "Theme2" ; // You can set the skin type through other conditions  
  20. return base.AreaViewLocationFormats.Select(f => f.Replace( "/Views/" , "/Views/" + theme + "/" ));
  21. }
  22. }
  23.  
  24. public override IEnumerable<string> ViewLocationFormats
  25. {
  26. get
  27. {


var value = new Random().Next(0, 1);
var theme = value == 0 ? "Theme1" : "Theme2"; // You can set the skin type through other conditions
return base.ViewLocationFormats.Select(f => f.Replace("/Views/", "/Views/" + theme + "/"));

  1. }
  2. }
  3. }
  4.  
  5. Then, the view engine can be replaced by modifying the instance property ViewEngines of MVcOptions. The code is as follows:
  6.  
  7. services.AddMvc().Configure<MvcOptions>(options =>
  8. {
  9. options.ViewEngines.Clear();
  10. options.ViewEngines.Add(typeof(ThemeViewEngine));
  11. });

In this way, when the system searches for view files, it will execute according to the logic of the newly registered ThemeViewEngine.
Control the View path through IViewLocationExpander

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:

  1. public   class ThemeViewLocationExpander : IViewLocationExpander
  2. {
  3. public   void PopulateValues(ViewLocationExpanderContext context)
  4. {
  5. var value = new Random().Next( 0 , 1 );
  6. var theme = value == 0 ? "Theme1" : "Theme2" ;
  7. context.Values[ "theme" ] = theme;
  8. }
  9.  
  10. public virtual IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context,
  11. IEnumerable<string> viewLocations)
  12. {
  13. return viewLocations.Select(f => f.Replace( "/Views/" , "/Views/" + context.Values[ "theme" ] + "/" ));
  14. }
  15. }

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:

  1. services.Configure<RazorViewEngineOptions>(options =>
  2. {
  3. options.ViewLocationExpanders.Add(typeof(ThemViewLocationExpander));
  4. });

<<:  Interpreting ASP.NET 5 & MVC6 Series (15): MvcOptions Configuration

>>:  Interpreting ASP.NET 5 & MVC6 Series (17): Other new features in MVC

Recommend

iOS 9: Quickly make your app support spotlight search

[[151508]] iOS9 supports indexing the content in ...

To combat climate change, would it be useful to form a “climate club”?

Produced by: Science Popularization China Author:...

In-depth report | Why are giants competing for information flow advertising?

There will inevitably be a battle between Toutiao...

Android Wear update: support for new gestures and listening to voice messages

According to foreign media reports, Google recent...

Advanced Numerology Course: Simple Eight Characters Master Edition

Advanced Numerology Course: Simple Eight Characte...

Yang Yuanqing: Motorola will return to the tablet market

Lenovo CEO Yang Yuanqing recently said that Lenov...

6000 words of essence, methodology to improve homepage conversion rate

In the second half of the Internet, when the traf...

Case analysis: Artbao’s event promotion process

In the past two years, if we were to ask which tr...

From 0 to 1, a complete analysis of the key points of APP from launch to promotion

As a newbie in the operation circle, I still don’...

Develop cross-platform HTML5 applications based on LeanCloud and Wex5

With the standardization of HTML5 technology, a g...

vivo Xplay 3S video and audio review

In terms of playing high-definition videos, domest...