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

A set of online event planning templates

As we enter 2022, the epidemic, which had been do...

Qiu Yuan-Victoria's Secret Body Shaping Private Lesson Video

The quality of the recorded video cannot be compa...

Why is it difficult to develop domestic chips? What are 7nm chips?

=========================================...

APP channel promotion: 3 major channels and experience sharing!

As the Matthew effect in the industry deepens, it...

Toyota finally can't sit still anymore

Ford Edge: Beats Highlander! Volkswagen Touron: B...