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

Baidu Search oCPC Product Manual

This article shares with you the latest "Bai...

Just a Size Increase? Apple iPad Pro Review

The iPad can be considered a "topic" pr...

Xi'an tea service is the best and highest quality

Xi'an High-Quality Tea Tasting Selection (WeC...

15 Free Ways to Promote Your Business Online

I have been engaged in the Internet industry for ...

up to date! Baidu 360, Sogou or Shenma search, which channel has more traffic?

Many people believe that China has a large popula...

Why do we put mosaics on the eyes?

Since its invention, mosaic has brought great con...

Analysis of Through Train Promotion Skills

What are the through train promotion techniques? ...

New discovery | Intestinal flora affects obesity!

Obesity is a global public health issue. Some stu...

Amazing! Do you always feel that "it rains when you wash your car"?

Have you ever had this experience? "It won&#...

Practical operation of Douyin account in K12 education industry!

As the proportion of short videos in the content ...