System.Web.Routing Introduction and Advanced _ practical skills

Source: Internet
Author: User
Tags httpcontext

System.Web.Routing is used to urlrouting in asp.net Web applications.
The so-called urlrouting is to map an address to another address, such as my visit to/chsword/2008/08/27.html is actually visited the/chsword/article.aspx?y=2008&m=08&d= 27 This address, urlrouting makes our URLs look very beautiful.

This article will be divided into 2 parts to introduce the use of urlrouting, respectively, entry and advanced.
the premise of the article:
1. Install the equivalent version of Visual Studio 2008 SP1 or other IDE.
2. Create a asp.net Web application (asp.net Web application)
3. Refer to the System.Web.Routing assembly.
The realization principle of urlrouting
If you're not the one who pursues the theory, it's just pragmatism that you can read directly from the preparatory work.
To make it easier for everyone to understand I've drawn a uml diagram in which a routecollection singleton pattern can be obtained by using the RouteTable routes property. Although it is not possible to determine the symbolic behavior of a singleton that is initialized when the value does not exist, it is initialized in the Application_Start event and is singleton mode until the application process is terminated.

and add route to routetable.routes in the following ways
ROUTETABLE.ROUTES.ADD (New Route ());
The above code only represents its process, and this code is not executed correctly because route does not provide an parameterless constructor.
Route is initialized with RouteValueDictionary to add default values and rules to route
In addition route also has a Iroutehandler implementation object, Iroutehandler implements the object to provide a Gethttphandler method to obtain the IHttpHandler which handles the URL.
So it's still at the abstract level, so here are some simple examples to take you through the urlrouting.
Preparatory work
Since we need a ihttphandler that handles URLs, we first define a class that implements the IHttpHandler interface.
Call it MyPage, because we want to interact with Iroutehandler, so in addition to implementing the IHttpHandler method, we also declare a requestcontext type of attribute.

Copy Code code as follows:

public class Mypage:ihttphandler {
Public RequestContext RequestContext {get; private set;}
Public MyPage (RequestContext context)
{
This. RequestContext = context;
}
#region IHttpHandler Members
public virtual void ProcessRequest (HttpContext context) {}
public bool IsReusable {
get {return false;}
}
#endregion
}

So we have a ihttphandler of our own.
Here we implement a Iroutehandler:
Copy Code code as follows:

public class Myroutehandler:iroutehandler {
#region Iroutehandler Members
Public IHttpHandler Gethttphandler (RequestContext requestcontext) {
return new MyPage (RequestContext);
}
#endregion
}

Here the Iroutehandler Gethttphandler method is implemented to return the mypage that we have just achieved.
So we finished the first 2 jobs, we can come to achieve urlrouting.
To achieve the first urlrouting
In fact, urlrouting after the definition of the two rules is very simple.
Write the following code in the Application_Start event Golbal.asax (no new one can be created)
Copy Code code as follows:

protected void Application_Start (object sender, EventArgs e) {
RegisterRoutes (routetable.routes);
}
public static void RegisterRoutes (RouteCollection routes) {
Routes. ADD (New Route ("{page}.aspx", new Myroutehandler ());
}

So we define the first urlrouting rule is to routing the URL of xxxx.aspx.
But we just define what kind of URLs we're dealing with, but we don't define what to do with them.
We should define how to handle this in the MyPage ProcessRequest method that we just defined.
We will implement the ProcessRequest method as follows:
Copy Code code as follows:

public virtual void ProcessRequest (HttpContext context) {
Context. Server.Execute (requestcontext.routedata.values["page"). ToString (). Replace ("_", "/") + ". aspx"
);
}

Obviously, the requestcontext.routedata.values["page" Here is the value of the page in the rule {page}.aspx. If I visit index.aspx then requestcontext.routedata.values["page" is index.
My definition here is to replace "_" with "/" so that the URL such as list_index.aspx will be transferred to a Web page such as list/index.aspx.
We set up some test pages as shown in the following illustration:

At any of these pages, write any text you want to tell the page.
Then automatically routing to the list/chsword.aspx when accessing the list_chsword.aspx.
Summarize the urlrouting with the following:
1. Rules defined in Application_Start
2. The IHttpHandler class that oneself realizes

So you for urlrouting even is a primer, the next article we will talk about some advanced settings.

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.