asp.net how MVC is run [2]: URL routing

Source: Internet
Author: User
Tags abstract foreach contains empty http request httpcontext split tostring

In a asp.net MVC application, the target object for each HTTP request is no longer a physical file as an ASP. NET Web Form Application for processing HTTP requests and for a corresponding action method that defines the controller type. It's some controller action. The name of the target Controller and action is contained in the HTTP request, and ASP.net MVC's first task is to get the correct controller and action names through parsing the current HTTP request. This process is accomplished by asp.net the URL routing mechanism of MVC.

First, Routedata

Asp. NET defines a global routing table in which each routing object in the routing table corresponds to a URL template that takes a controller and an action name as a stance character. For each HTTP request that arrives, ASP.net MVC traverses the routing table to find a URL template that matches the path to the request address, and ultimately resolves the routing data with Controller and action names as the core. In our custom asp.net MVC framework, the routing data is represented by a routedata type with the following definitions.

1:public class Routedata
2: {
3:public idictionary<string, object> Values {get; private set;}
4:public idictionary<string, object> datatokens {get; private set;}
5:public Iroutehandler Routehandler {get; Set }
6:public routebase Route {get; set;}
7:
8:public Routedata ()
9: {
10:this. Values = new dictionary<string, object> ();
11:this. Datatokens = new dictionary<string, object> ();
12:this. Datatokens.add ("Namespaces", New List<string> ());
13:}
14:public string Controller
15: {
16:get
17: {
18:object controllername = string. Empty;
19:this. Values.trygetvalue ("Controller", out controllername);
20:return controllername.tostring ();
21:}
22:}
23:public string ActionName
24: {
25:get
26: {
27:object ActionName = string. Empty;
28:this. Values.trygetvalue ("Action", out ActionName);
29:return actionname.tostring ();
30:}
31:}
32:public ienumerable<string> namespaces
33: {
34:get
35: {
36:return (ienumerable<string>) this. Datatokens["Namespaces"];
37:}
38:}
39:}

As shown in the code snippet above, Routedata defines the attributes values and Datatokens of two dictionary types, which represent variables that are resolved directly from the request address, which represent other types of variables. The attribute with the same name as the Controller and action name is extracted directly from the values dictionary and the corresponding key is controller and action. The attribute namespaces represents a list of namespaces that are set for the resolution of the secondary controller type, which is extracted from the Datatokens dictionary and the corresponding key is namespaces.

We've mentioned ASP.net before. MVC is essentially two custom asp.net components, one is a custom HttpModule, the other is a custom HttpHandler, which is obtained from the Routedata routehandler attribute. The Routedata Routehandler property type is the Iroutehandler interface, as shown in the following code fragment, which has a unique gethttphandler for returning the HttpHandler object that is actually used to process the HTTP request.

   1:public Interface Iroutehandler
2: {
3: IHttpHandler Gethttphandler (RequestContext requestcontext);
4:}

The Gethttphandler method of the Iroutehandler interface accepts a parameter of type RequestContext. As the name suggests, RequestContext represents the context of the current (HTTP) request, and its core is the encapsulation of the current HttpContext and Routedata, which can be seen in the following code snippet.

   1:public class RequestContext
2: {
3: Public virtual httpcontextbase HttpContext {get; set;}
4: Public virtual Routedata routedata {get; set;}
5:}

Ii. Route and RouteTable

Routedata has a route property of type RouteBase that represents the routing object in the current routing table that matches the current request. In other words, the current routedata is obtained by parsing the routing object against the current HTTP request. RouteBase is an abstract class, as shown in the following code snippet, which contains only a Getroutedata method that obtains a Routedata object by parsing the current HTTP context represented by a HttpContextBase object.

   1:public Abstract class RouteBase
2: {
3: Public abstract Routedata getroutedata (HttpContextBase HttpContext);
4:}

asp.net MVC provides a routing mechanism based on a URL template that is implemented through a route type that has the following definitions. Route is a subclass of RouteBase, and the URL property of a string type represents a defined URL template. In the implemented Getroutedata method, the relative request address is obtained by httpcontextbase, and if the address matches the URL pattern defined in the template, a routedata return is created, otherwise null is returned. For the returned Routedata object, the dictionary represented by the values attribute contains variables that are resolved directly through the address, and for Datatokens dictionaries and Routehandler properties, it is taken directly from the route object's identically named attribute.

1:public class Route:routebase
2: {
3:public Iroutehandler Routehandler {get; set;}
4:public Route ()
5: {
6:this. Datatokens = new dictionary<string, object> ();
7:this. Routehandler = new Mvcroutehandler ();
8:}
9:public override Routedata Getroutedata (HttpContextBase HttpContext)
10: {
11:idictionary<string, object> variables;
12:if (this. Match (HttpContext.Request.AppRelativeCurrentExecutionFilePath.Substring (2), out variables))
13: {
14:routedata routedata = new Routedata ();
15:foreach (var item in variables)
16: {
17:routedata.values.add (item. Key, item. Value);
18:}
19:foreach (var item in Datatokens)
20: {
21:routedata.datatokens.add (item. Key, item. Value);
22:}
23:routedata.routehandler = this. Routehandler;
24:return Routedata;
25:}
26:return null;
27:}
28:public string Url {get; set;}
29:public idictionary<string, object> datatokens {get; set;}
30:protected bool Match (String requesturl, out idictionary<string,object> variables)
31: {
32:variables = new dictionary<string,object> ();
33:string[] StrArray1 = requesturl.split ('/');
34:string[] StrArray2 = this. Url.split ('/');
35:if (strarray1.length!= strarray2.length)
36: {
37:return false;
38:}
39:
40:for (int i = 0; i < strarray2.length; i++)
41: {
42:if (Strarray2[i]. StartsWith ("{") && Strarray2[i]. EndsWith ("}"))
43: {
44:variables. ADD (Strarray2[i]. Trim ("{}"). ToCharArray ()), strarray1[i]);
45:}
46:}
47:return true;
48:}
49:}

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.