Basic usage of system. Web. Routing

Source: Internet
Author: User

With the release of ASP. net mvc beta and. net3.5 SP1, The system. Web. Routing component has officially met you. Here we will briefly introduce how to use routing for URL routing (URL rewriting ).

Suppose we want to access URL: http: // localhost: 4541/123456. aspx

But the actual URL: http: // localhost: 4541/product. aspx? Id = 123456

In this way, we need to process the URL during the request URL. If system. Web. Routing?

1. Install the. net3.5 SP1 Patch. Of course, you can copy this component directly.

2. reference the system. Web. Routing component in a web project.

3. Implement ihttphandler. We create a producthttphandler to implement the ihttphandler interface and save a requestcontext object in the implementation.

Public   Class Producthttphandler: ihttphandler
{
Public Requestcontext { Get ; Private   Set ;}
Public Producthttphandler (requestcontext context)
{
This . Requestcontext = Context;
}

Public   Void Processrequest (httpcontext context)
{
String URL =   String . Format ( " Product. aspx? Id = {0} " , Requestcontext. routedata. Values [ " ID " ]);
Context. server. Execute (URL );
}

Public BoolIsreusable {Get{Return False;}}

}

Note:

#. The requestcontext object is a class in the routing component.

#. Saves a requestcontext object that can only be initialized by the constructor.

#,The processrequest method creates a URL for real access and then submits it to the server. Execute Method for execution.

#. Read the actually matched URL parameters in the routedata dictionary.

#. To perform the test, you need to create the product. ASPX page and return the ID parameter to facilitate the test:

Protected   Void Page_load ( Object Sender, eventargs E)
{
Response. Write (request. querystring [
" ID " ]);
}

4. Implement iroutehandler. We need an iroutehandler interface to add a route object to the system's routecollection set. Therefore, we create a productroutehandler implementation like this.

Public   Class Productroutehandler: iroutehandler
{
Public Ihttphandler gethttphandler (requestcontext)
{
Return   New Producthttphandler (requestcontext );
}
}

In implementation, gethttphandler does nothing, that is, it instantiates an ihttphandler implementation we just created. In fact, it is necessary to give the control permission to ihttphandler for processing.

5. initialize the route table in the application_start event of Global. asax. CS.

Public   Class Global: system. Web. httpapplication
{
Public   Static   Void Registerroutes (routecollection routes)
{
Routes. Add (
New Route ( " {ID}. aspx " , New Productroutehandler ()));
}

Protected VoidApplication_start (ObjectSender, eventargs E)
{
Registerroutes (routetable. routes );
}
}

#, Routetable. routes is a read-only attribute, it returns a single pieceRoutecollection

 

#. By adding a series of routing rules to the routecollection, the system automatically selects the appropriate URL for the request.

6. If you create a common website or web application, you may not automatically configure the system. Web. Routing Group line. You can manually configure it as follows:

< Compilation Debug = "False" >
< Assemblies >
< Add Assembly = "System. Web. Routing, version = 3.5.0.0, culture = neutral,
Publickeytoken = 31bf3856ad364e35"
/>
</ Assemblies >
</ Compilation >

< Pages >
< Namespaces >
< Add Namespace = "System. Web. Routing" />
</ Namespaces >
</ Pages >

< System. Web >
< Httpmodules >
< Add Name = "Urlroutingmodule" Type = "System. Web. Routing. urlroutingmodule,
System. Web. Routing, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"
  />
</ Httpmodules >
</ System. Web >

< System. Webserver >
< Modules Runallmanagedmodulesforallrequests = "True" >
< Remove Name = "Urlroutingmodule"   />
< Add Name = "Urlroutingmodule" Type = "System. Web. Routing. urlroutingmodule,
System. Web. Routing, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"
  />
</ Modules >
</ System. Webserver >

Note: to add an httpmodule, you must configure it in the system. Web and system. webserver sections.

Now, when you browse the http: // localhost: 4541/123456. ASPX page, 123456 is displayed.

Follow-up:

Regular Expressions

Route can also use regular expressions. Only URLs in the specified format can participate in the route of this route and grant the access permission to the specified page.

Registerroutes
Public   Static   Void Registerroutes (routecollection routes)
{
// Regular Expression
Routes. Add ( " R2 " , New Route ( " {ID}. aspx " ,
New Routevaluedictionary ( New {ID =   " 1 " }),
New Routevaluedictionary ( New {ID = " [\ D] + " }),
New Productroutehandler ()));
}

# FirstRoutevaluedictionary is used to set the default values of parameters in the route URL template. This dictionary can also use this channel to transmit the values to be configured.Ihttphandler implementation.

# SecondRoutevaluedictionary is used to set the parameter format. Here, a regular expression is used. Only the URL in the Union format will be processed by this route. Otherwise, it will be moved down to the next route.

At this time, if you access http: // localhost: 4541/ABC. aspx, the Error 404 will be reported, and access to 123. aspx will be normal.

About route table Sequence

As we can see from the above description, the URL Processing is matched in the order from top to bottom, and the matching URL is processed by the current route, the URL that does not match will be moved down to the next route to match.

Therefore, if we add a format that is easier to match before the registration method, the second route will never be executed.

Registerroutes
Public   Static   Void Registerroutes (routecollection routes)
{
Routes. Add (
" R1 " , New Route ( " {ID}. aspx " , New Productroutehandler ()));
// Regular Expression
Routes. Add ( " R2 " , New Route ( " {ID}. aspx " ,
New Routevaluedictionary ( New {ID =   " 1 " }),
New Routevaluedictionary ( New {ID = " [\ D] + " }),
New Productroutehandler ()));
}

 

Unfinished .....

# Virtualpath

How do we useProgramGenerate a URL in the specified format instead of being constructed manually?

 

# Httpmethodconstraint

 

 

 

 

 

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.