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.
- void RegisterRoutes(RouteCollection routes)
- {
- 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:
- void RegisterRoutes(RouteCollection routes)
- {
- routes.MapPageRoute("SalesRoute",
- "SalesRoute/{year}",
-
- "~/sales.aspx", true,
- new RouteValueDictionary{
- { "year", DateTime.Now.Year.ToString()}},
- new RouteValueDictionary{
- { "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.
- protected void Page_Load(object sender, EventArgs e)
- {
- 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:
- RouteValueDictionary parameters = new RouteValueDictionary
- {
- { "year", "2008" },
- { "category", "recreation" }
- };
- VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "SalesRoute", parameters);
- 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
- Disable ASP. NET midway through to check whether it affects server execution
- Disable the ASP. net bug in the browser sessionfor 1 minute
- Overview ASP. NET page framework
- Describe ASP. NET page forms
- ASP. NET page Object Model