Interpretation of ASP.net 5 & MVC6 Series (16): Customizing View View File Lookup Logic _ self-study Process

Source: Internet
Author: User

Before MVC5 and previous versions, we wanted to control the path to the view file, we had to IViewEngine override the interface's FindPartialView or method, and FindView all the view engines were inherited from the IViewEngine interface, such as the default RazorViewEngine . But in the new version MVC6, the path to the view file is not quite the same, there are currently two ways, one is through RazorViewEngine , the other is through the new feature IViewLocationExpander interface.

To control the view path by Razorviewengine

In the new version RazorViewEngine , the class provides two virtual properties ( AreaViewLocationFormats and ViewLocationFormats ) that can be used to override controls without having to override the FindPartialView or FindView methods, as shown in the following example:

public class Themeviewengine:razorviewengine {public Themeviewengine (irazorpagefactory pagefactory, IRAZORVIEWFA Ctory viewfactory, Iviewlocationexpanderprovider viewlocationexpanderprovider, Iviewlocationcache ViewLocationCach

  E): Base (Pagefactory, Viewfactory, Viewlocationexpanderprovider, Viewlocationcache) {} public override ienumerable<string> Areaviewlocationformats {get {var value = new Random ().
      Next (0, 1); var theme = value = = 0? "Theme1": "Theme2"; Other conditions can be used to set the type of skin return base.
    Areaviewlocationformats.select (F => f.replace ("/views/", "/views/" + theme + "/")); } public override ienumerable<string> Viewlocationformats {get {var value = new Random ().
      Next (0, 1); var theme = value = = 0? "Theme1": "Theme2"; Other conditions can be used to set the type of skin return base.
    Viewlocationformats.select (F => f.replace ("/views/", "/views/" + theme + "/")); }
  }
}

You can then complete the substitution of the view engine by modifying the Mvcoptions instance Properties Viewengines, as follows:

Services. Addmvc (). configure<mvcoptions> (Options =>
{
  options). Viewengines.clear ();
  Options. Viewengines.add (typeof (Themeviewengine));

In this way, when the system finds the view file, it executes according to the new registered ThemeViewEngine logic.

To control the view path by Iviewlocationexpander

In MVC6, Microsoft also provides a new way to control the path of the view file, which is the IViewLocationExpander interface that implements the custom logic by implementing the interface, and can also use the relevant context object. Examples are as follows:

 public class Themeviewlocationexpander:iviewlocationexpander {public void Populat Evalues (Viewlocationexpandercontext context) {var value = new Random ().
    Next (0, 1); var theme = value = = 0?
    "Theme1": "Theme2"; Context.
  values["theme"] = theme;
                              Public virtual ienumerable<string> expandviewlocations (Viewlocationexpandercontext context, Ienumerable<string> viewlocations) {return viewlocations.select (f => f.replace ("/views/", "/Vie") ws/"+ context.
  values["Theme"] + "/"); }
}

In the above customization IViewLocationExpander , 2 methods are implemented, and the method allows us to add a response in the context of the PopulateValues ExpandViewLocations PopulateValues ViewLocationExpanderContext key value pairs for subsequent use, through which we can use the context object to find ActionContext and HttpContext object, In order to use these objects to respond to the judgment operation, and the ExpandViewLocations method, only in the absence of the view cache or in the view cache can not find the corresponding key of the view file to call the method, in this method, we could dynamically return the location of the view.

Finally, we Startup.cs implement the registration purpose by modifying the RazorViewEngineOptions properties of the instance object in the ViewLocationExpanders following code:

Services. configure<razorviewengineoptions> (Options =>
{
  options). Viewlocationexpanders.add (typeof (Themviewlocationexpander));
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.