Learning ASP. NET MVC5 framework secrets notes-ASP. net mvc routing (4), mvc5-asp.net

Source: Internet
Author: User

Learning ASP. NET MVC5 framework secrets notes-ASP. net mvc routing (4), mvc5-asp.net
2.2.4 Area-based route ing

For a large-scale Web application, we can divide it into smaller units through Area. Each Area is equivalent to an independent subsystem, which has a set of directory structures and configuration files including Models, Views, and Controller. Generally, each Area has its own routing rules (the routing template usually contains the name of the Area), and the Area-based routing ing is registered through the AreaRegistration type.

1. AreaRegistration and AreaRegistrationContext

AreaRegistration is used to register routes for an Area. As shown in the following code, AreaRegistration is an abstract class. Its abstract read-only attribute AreaName returns the name of the current Area. The abstract method RegisterArea is used to implement route registration based on the current Area.

public abstract class AreaRegistration{public static void RegisterAllAreas();public static void RegisterAllAreas(object state);public abstract string AreaName { get; };public abstract void RegisterArea(System.Web.Mvc.AreaRegistrationContext context);}

AreaRegistration defines two abstract static RegisterAllAreas method overloading. The parameter state indicates the data passed to the specific AreaRegistration. When the RegisterAllArea method is executed, all programs directly or indirectly referenced by the current Web application are loaded (if not yet loaded), ASP. net mvc parses all types inherited from AreaRegistration from these programs and creates corresponding AreaRegistration objects through reflection. For each created AreaRegistration object, an AreaRegistrationContext object that serves as the registration context of the Area will be created, it is used as a parameter to call the RegisterArea method of these AreaRegistration objects to register routes for the corresponding Area.

As shown in the following code snippet, the read-only attribute AreaName of AreaRegistrationContext indicates the name of the Area. The attribute Routes is a RouteCollection object representing the route table, and the State is a user-defined object, they all initialize through constructors. Specifically, the AreaRegistrationContext object is constructed when you call the static method RegisterAllAreas of AreaRegistration. Its AreaName comes from the same name attribute of the current AreaRegistration object, routes corresponds to the global route table expressed by Routes, which is the static attribute of RouteTable. The state parameter specified to call the RegisterAllAreas method will be used as the same name parameter to call the AreaRegistrationContext constructor.

public class AreaRegistrationContext{public AreaRegistrationContext(string areaName,RouteCollection routes, object state);public AreaRegistrationContext(string areaName,RouteCollection routes);public Route MapRoute(string name, string url, object defaults, object constraints, string[] namespaces);public Route MapRoute(string name, string url, object defaults, string[] namespaces);public Route MapRoute(string name, string url, string[] namespaces);public Route MapRoute(string name, string url, object defaults, object constraints);public Route MapRoute(string name, string url, object defaults);public Route MapRoute(string name, string url);public string AreaName { get; }public ICollection<string> Namespaces { get; }public RouteCollection Routes { get; }public object State { get; }}


The read-only attribute Namespaces of AreaRegistrationContext indicates a group of Namespaces that need to be matched first (when multiple Controller types with the same name are defined in different Namespaces, the Controller type defined in these namespaces is preferred ). When an AreaRegistrationContext object for a specific AreaRegistration is created, if the AreaRegistration type is defined in a namespace (such as "Artech. controllers), then add ". * "indicates the string generated by the suffix (for example," Artech. controllers. *) is added to the Namespaces collection. In other words, for multiple controllers of the same name defined in different namespaces, the Controller that is included in the namespace of the current AreaRegistration is preferred.

AreaRegistrationContext defines a series of MapRoute methods for Route registration. The usage and parameter Meanings of the methods are consistent with those of RouteCollection class extension methods of the same name. Note that if the MapRoute method does not specify a namespace, The namespace list indicated by the Namespaces attribute will be used. Otherwise, the namespace contained in this attribute is directly ignored.

When we use ASP. net mvc Project template when creating a Web application. the asax file generates code similar to the following. Here, you can register all the areas by calling the static method RegisterAllAreas of AreaRegistration. That is to say, registration for all areas occurs when the Web application is started.

Protected void Application_Start () {AreaRegistration. RegisterAllAreas (); // other operations}


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.