Detailed description of URL Routing in ASP. NET 4

Source: Internet
Author: User
Tags net bug

This article will start with the concept of URL Routing and introduce the functions of URL Routing in ASP. NET 4.0. I hope this article will help you.

What is URL Routing?

First, URL routing is actually implemented in ASP. NET 5 SP1 has been introduced, but it has never been used before. Besides, Microsoft has a lot of information about ASP. NET 4 takes this as a new feature, so let's take the question as this.
Previously, a typical ASP. net url would typically look like the http://www.myexample.com/salesreport.aspx below? Year = 2009

In this URL, salesreport. aspx represents a physical file that actually exists. The existence of suffix. aspx in the url is meaningless, and the url is not SEO-friendly. With the use of URL Routing, we can use the following more concise and clear address to access, http://www.myexample.com/salesrepot/2009

Use Route Engine for URL Mapping

In asp.net mvc, the url is mapped to the corresponding controller and action through MapRoute. In web form. in assx Application_Start, we map the url to a page through MapPageRoute.

 
 
  1. void RegisterRoutes(RouteCollection routes)   
  2. {      
  3. routes.MapPageRoute("SalesRoute", "SalesReport/{year}", "~/sales.aspx");                                  

MapPageRoute uses three parameters. The first parameter is the Route name, the second parameter is the ing Pattern of the URL, and the last parameter is the corresponding ASPX page. In addition to this most common direct method, you can also use other methods of its overload to set the default value for a route and add various constraints, as shown below:

 
 
  1. void RegisterRoutes(RouteCollection routes)   
  2. {   
  3.  routes.MapPageRoute("SalesRoute",   
  4.                         "SalesRoute/{year}",   
  5.                           
  6. "~/sales.aspx", true,   
  7.                        new RouteValueDictionary{   
  8.                             { "year", DateTime.Now.Year.ToString()}},                                            
  9.                         new RouteValueDictionary{   
  10.                            { "year", @"\d{4}" } });   

Compared with the first route, this parameter has three more parameters. The first parameter is a boolean value, and the last two parameters are RouteValueDictionary. The first parameter specifies a default value, the last one is a constraint, and the regular expression is used to constrain that the year parameter must be a four-digit number.

After successfully ing SalesRoute/2009 to the sales. aspx page, how can I get the "2009" value in the sales code? Obtain the RouteData of the Page class.

 
 
  1. protected void Page_Load(object sender, EventArgs e)   
  2. {       
  3. string year = RouteData.Values["year"] as string;                                                    

Use Routing Engine to generate a URL

In addition to parsing URLs, we can also generate these concise and clear URLs, as shown in the following code:

 
 
  1. RouteValueDictionary parameters = new RouteValueDictionary   
  2. {   
  3. { "year", "2008" },   
  4. { "category", "recreation" }  
  5. };    
  6. VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "SalesRoute", parameters);                      
  7. hyperLnk.NavigateUrl = vpd.VirtualPath; 

The SalesRoute here is the Route we registered in Application_Start. It is worth noting that in the Pattern of SalesRoute, we do not have "{category}". How does RouteEngine process this value? At this point, category is added to the url as a querystring, the generated URL will be like this: http://www.myexample.com/salesreport/2009? Category = recreation

In addition, if a button is added to the sales page, will the url corresponding to the button be as concise as the preceding url? The answer is yes. However, you can specify the Action in the Form tag to implement page sending back.

Summary

Although the newly added Route function does not make Asp. Net Web Form as exciting as Asp. Net MVC, it is at least a good function.

Original article title: Experience URL Routing of ASP. NET 4

Link: http://www.cnblogs.com/jun1st/archive/2009/10/25/aspnet_urlrouting.html

  1. Disable ASP. NET midway through to check whether it affects server execution
  2. Disable the ASP. net bug in the browser sessionfor 1 minute
  3. Overview ASP. NET page framework
  4. Describe ASP. NET page forms
  5. ASP. NET page Object Model

Related Article

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.