Improvement on URL rewriting in net4.0-ASPnet

Source: Internet
Author: User

There are many benefits of URL rewriting, such as Seo, memory-friendly, hiding real paths to improve security and update convenience. In versions earlier than aspnet4.0, URL rewriting is complicated, and sometimes third-party class libraries are used, which can be implemented in aspnet4.0, the following describes how to rewrite a URL in aspnet4.0.

1. Open vs2010 and create an ASPnet project named urlrewriterdemo.

We can see that the project structure in aspnet4 is a bit similar to ASPnet MVC. With a lot of ready-to-use items added, you can simply change them into a simple personal site.

2. Open the global. asax file and reference the namespace.System. Web. Routing,System. Web. RoutingThe namespace already exists in net3.5, but it is not as powerful as 4.0. Add the following method to the file:

void RegisterRoutes(RouteCollection routes){    routes.MapPageRoute("productdetail",         "products/{category}/{name}",        "~/test.aspx", false,        new RouteValueDictionary { { "category", "book" }, { "name", "aspnet" } });}

Description of parameters of the mappageroute method:

"Productdetail": route name, which must match the current route settings.

"Products/{category}/{name}": URL rewriting rule.

"~ /Test. aspx ": configure the corresponding physical file.

New routevaluedictionary {"category", "book" },{ "name", "ASPnet" }}: configure the default value.

3. Add the following code to the method application_start in global. asax:

void Application_Start(object sender, EventArgs e){    RegisterRoutes(RouteTable.Routes);}

4. Add a page named test. aspx to the project. If it is named another name, set the parameter of the physical file configured in the mappageroute method above to the corresponding name. Add the following content to the test. ASPX page:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">    

5. Set the project as a fixed port, such as 10000. Press F5 to run the project and change the URL address to http: // localhost: 10000/products, products is the name configured in the URL rewrite rule. The running result is as follows:

6. We have configured the default parameter values category and name in registerroutes. To obtain the parameter values on the page, you can use routedata. Values to modify the code on the test. ASPX page as follows:

<Asp: Content ID = "content2" contentplaceholderid = "maincontent" runat = "server"> 

7. On the refresh page, you can see that the default values configured in registerroutes are displayed on the page:

8. Change the URL to http: // localhost: 10000/products/computer/ThinkPad. Then, refresh the page and you will see the category name and product name set in the URL:

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.