System.Web.Routing Introduction and Advanced Chapter

Source: Internet
Author: User

System.Web.Routing has been published as an assembly in . Net3.5sp1. Although we did not find the trace of ASP. NET MVC in 3.5sp1 , we also felt that it was not far from us.

The System.Web.Routing is used to urlrouting in an ASP. NET Web application .

The so-called urlrouting is to map an address to another address, such as my visit to/chsword/2008/08/27.html is actually a visit to/chsword/article.aspx?y=2008&m=08&d= 27 This address, urlrouting makes our URL look very beautiful.

This article will be divided into 2 parts to introduce the use of urlrouting, respectively, the entry and advanced chapter.

The premise of the article:

1. Install the equivalent version of Visual Studio SP1 or other ides .

2. Build an ASP. NET Web application(ASP. NET Web application)

3. Reference the System.Web.Routing assembly.

Urlroutingthe implementation principle

If you are not a person who pursues a theory, it is only pragmatism that can be read directly from the preparatory work.

to make it easy for everyone to understand I've drawn a UML diagram, in which a routecollection Singleton pattern can be obtained by Routes this property of routetable. Although there is no indication that the value does not exist , the Singleton behavior is initialized, but it is initialized in the Application_Start event, and until the application process is terminated, it is Singleton mode.

In the following ways, the Route added to routetable.routes

routetable . Routes. ADD (New Route ());

The code above only represents its process, which is not executed correctly because the route does not provide a parameterless constructor.

When the route is initialized , it uses routevaluedictionary to add default values and rules to the route .

also The Route also has a Iroutehandler implementation object, and the Iroutehandler implementation object provides a Gethttphandler method to get the The IHttpHandler of the URL.

so we're still at the level of abstraction, so let's take a few simple examples to get you started . Urlrouting.

preparatory work

due to the need for a processing Url IHttpHandler So we first define a class that implements the IHttpHandler interface.

Just call it MyPage, because we want to interact with Iroutehandler , In addition to implementing the IHttpHandler method, we also declare a RequestContext types of properties .

Public classMypage:ihttphandler {
PublicRequestContext RequestContext {Get; Private Set; }
PublicMyPage (RequestContext context)
{
This. RequestContext=context;
}
#regionIHttpHandler Members
Public Virtual voidProcessRequest (HttpContext context) {}
Public BOOLIsReusable {
Get { return false; }
}
#endregion
}

so we have one of our own . IHttpHandler.

below we implement a Iroutehandler:

     Public classMyroutehandler:iroutehandler {
#regionIroutehandler Members
PublicIHttpHandler Gethttphandler (RequestContext requestcontext) {
return NewMyPage (RequestContext);
}
#endregion
}

here is the realization Iroutehandler 's Gethttphandler method to return to the mypage we realized just now .

so we're done with 2 of our early jobs, and we can do it. it's urlrouting.

implementation of the firstUrlrouting

actually Urlrouting is simple after defining the two rules.

in the Golbal.asax(no new one can be created) write the following code in the Application_Start event

protected voidApplication_Start (Objectsender, EventArgs e) {
RegisterRoutes (routetable.routes);
}
Public Static voidregisterroutes (routecollection routes) {
Routes. ADD (NewRoute ("{page}.aspx",NewMyroutehandler ()));
}

so we're going to define the first one. The urlrouting rule is to Routing URLs such as xxxx.aspx.

but we're just defining what we have to deal with. Url, but does not define what to do with it.

we should have just defined the the ProcessRequest method in MyPage defines how to handle it.

we will The ProcessRequest method is implemented as follows:

Public Virtual voidProcessRequest (HttpContext context) {
Context. Server.Execute (requestcontext.routedata.values["page"]. ToString (). Replace ("_","/")+". aspx"
);
}

It's obvious here requestcontext . values [" page "] . {Page}.aspx page The value is, if I access Index.aspx routedata. values [" page "] index

my definition here is to replace "_" with "/" so it's going to go to list/index.aspx with a URL like list_index.aspx. On such a webpage.

We set up some test pages as shown in:

On these pages, feel free to write text that distinguishes the page.

the list_chsword.aspx is automatically Routing to the list/chsword.aspx when it accesses the .

Summarize Urlrouting is related to the following:

1. rules defined in the Application_Start

2. self-fulfilling IHttpHandler class

In this way, if you are getting started with urlrouting, we will talk about some advanced settings in the future.

Expected content:

    • Default values for rules
    • Regular rule settings
    • Tips for use

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.