Asp.net 4.0 URL routing experience

Source: Internet
Author: User

Just like just now, do some small experiments and feel the convenience brought by the new version. Let alone other things.

 

Step 1:

Add Rules in the global. asax File

Void application_start (Object sender, eventargs E)
{
// Code that runs when the application starts
Registerroutes (system. Web. Routing. routetable. routes );
}
Void registerroutes (system. Web. Routing. routecollection routes)
{
Routes. mappageroute ("myfriendlyname", "Report/{year }","~ /A. aspx ");
}

 

Add <a href = "report/2010"> test </a> to the default. aspx page. click the button and pay attention to the address bar. Let's take a look at the content on the. ASPX page.

Public partial class A: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Response. Write (routedata. Values ["year"] as string );

If (request. querystring ["category"]! = NULL)
{
Response. Write (request. querystring ["category"]. tostring ());
}
}
}

 

 

You can also generate a friendly name by yourself:

Routevaluedictionary parameters = new routevaluedictionary ()
{
{"Year", "2008 "},
{"Category", "goods "},
{"Customer", "hello "}
};

Virtualpathdata VPD = routetable. routes. getvirtualpath (null, "myfriendlyname", parameters );
This. hyperlink1.navigateurl = VPD. virtualpath;

 

At this time, the address is changed to: http: // localhost: 9319/website1/report/2008? Category = Goods & customer = Hello

Let's take a look at category and customer. These two are the corresponding parameters.

 

It should be convenient. In the past, you also needed to configure IIS to achieve the corresponding functions.

 

 

 

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.