ASP. net mvc -- Routing

Source: Internet
Author: User

The main responsibility of routing is to map browser requests to the controller action of MVC.

Routing-related classes in. NET:

 

 

 


A route consists of two parts: Route registration and request ing:

 

1. Route registration:

Route registration is simple, that is, adding a route to the route table (RouteCollection:

[Csharp]
Public static void RegisterRoutes (RouteCollection routes)
{
Routes. IgnoreRoute ("{resource}. axd/{* pathInfo }");
 
Routes. MapRoute (
Name: "Default ",
Url: "{controller}/{action}/{id }",
Defaults: new {controller = "Home", action = "Index", id = UrlParameter. Optional}
);
}

Public static void RegisterRoutes (RouteCollection routes)
{
Routes. IgnoreRoute ("{resource}. axd/{* pathInfo }");

Routes. MapRoute (
Name: "Default ",
Url: "{controller}/{action}/{id }",
Defaults: new {controller = "Home", action = "Index", id = UrlParameter. Optional}
);
}


 

Ii. Request ing:

In ASP. net mvc, request ing is implemented through custom IHttpModule,

[Csharp]
HttpContextWrapper httpContext = new HttpContextWrapper (HttpContext. Current );
RouteData routeData = routes. GetRouteData (httpContext );
If (routeData = null)
{
Return;
}
 
RequestContext context = new RequestContext () {HttpContext = httpContext, RouteData = routeData };
IHttpHandler handler = routeData. RouteHandler. GetHttpHandler (context );
HttpContext. RemapHandler (handler );

HttpContextWrapper httpContext = new HttpContextWrapper (HttpContext. Current );
RouteData routeData = routes. GetRouteData (httpContext );
If (routeData = null)
{
Return;
}

RequestContext context = new RequestContext () {HttpContext = httpContext, RouteData = routeData };
IHttpHandler handler = routeData. RouteHandler. GetHttpHandler (context );
HttpContext. RemapHandler (handler );

After the route table is mapped, the returned RouteData contains information such as Controller and Action. Then, package the information and send it to HttpHandler for processing.

 

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.