Interpretation of ASP. NET 5 & MVC6 series of tutorials (16): Custom View File Search logic, interpretation of ASP. NET

Source: Internet
Author: User

Interpretation of ASP. NET 5 & MVC6 series of tutorials (16): Custom View File Search logic, interpretation of ASP. NET

In MVC5 and earlier versions, if we want to control the path of the View File, we mustIViewEngineInterfaceFindPartialViewOrFindViewMethod, all view engines inherit from thisIViewEngineInterface, such as the defaultRazorViewEngine. However, in the new version of MVC6, the path to the view file is not the same. Currently, there are two ways:RazorViewEngine, The other is through the new featuresIViewLocationExpanderInterface.

Use RazorViewEngine to control the View path

In the new versionRazorViewEngineThe class provides two virtual attributes (AreaViewLocationFormatsAndViewLocationFormats), Which can be used to override the control without havingFindPartialViewOrFindViewMethod, for example:

Public class ThemeViewEngine: RazorViewEngine {public ThemeViewEngine (extends pageFactory, IRazorViewFactory viewFactory, implements callback, IViewLocationCache viewLocationCache): base (pageFactory, viewFactory, callback, viewLocationCache) {} public override IEnumerable <string> AreaViewLocationFormats {get {var value = new Random (). Next (0, 1); var theme = value = 0? "Theme1": "Theme2"; // you can set another condition to return base for the skin type. 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"; // you can set another condition to return base for the skin type. viewLocationFormats. select (f => f. replace ("/Views/", "/Views/" + theme + "/"));}}}

Then, you can replace the view engine by modifying the ViewEngines instance attribute of MVcOptions. The Code is as follows:

services.AddMvc().Configure<MvcOptions>(options =>{  options.ViewEngines.Clear();  options.ViewEngines.Add(typeof(ThemeViewEngine));});

In this way, the system will follow the newly registeredThemeViewEngine.

Use IViewLocationExpander to control the View path

In MVC6, Microsoft also provides another new method to control the path of the View File, that isIViewLocationExpanderInterface. You can implement the custom logic by implementing this interface, and you can also use the relevant context object. Example:

public class ThemeViewLocationExpander : IViewLocationExpander{  public void PopulateValues(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/", "/Views/" + context.Values["theme"] + "/"));  }}

In the preceding customIViewLocationExpander, Two methods are implemented:PopulateValuesAndExpandViewLocations,PopulateValuesThe method allows us to think aboutViewLocationExpanderContextAdd the response key-value pairs in the context for later use. Through this context object, we can findActionContextAndHttpContextObjects to use these objects for response judgment.ExpandViewLocationsThis method is called only when there is no View cache or the View file of the corresponding key cannot be found in the View cache. In this method, the position of the View can be dynamically returned.

Finally, weStartup.csBy modifyingRazorViewEngineOptionsInstance ObjectViewLocationExpandersTo achieve the purpose of registration, the Code is as follows:

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.