Before. Net 4.0, in order to make search engine-friendly and user-friendly URLs, We need to rewrite the URLs by ourselves. Now we don't need it. Net 4.0 is doing this for us. UrlRouting is called Routing because it not only implements Url rewriting, but also obtains the rewritten Url through parameters for use on the page.
1. Common Use of Url Routing
UrlRouting is widely used in Asp. Net Mvc projects and can be used in Mvc. Therefore, it is transplanted to webform. Let's take a look at the usage in webform first.
Assume a scenario: we need to create a daily blog article page. The url we want is:
/Archive/2010/05/10/default. aspx
This address will be mapped ~ /Posts. aspx File
To use UrlRouting, You need to register the UrlRouting rule to the RouteTable. the following code is used to register the Routing rule in the Global file:
01
public
static
void
RegisterRoutes(RouteCollection routes)
02
{
03
routes.Ignore(
"{resource}.axd/{*pathInfo}"
);
04
05
routes.MapPageRoute(
"blogs"
,
// Name the UrlRouting rule
06
"archive/{year}/{month}/{date}/default.aspx"
,
// The desired friendly Url format
07
"~/blogs.aspx"
,
// Map to the aspx page path
08
false
,
// Check User Permissions
09
new
RouteValueDictionary{ {
"year"
, DateTime.Now.Year },
10
{
"month"
, DateTime.Now.Month },
11
{
"date"
, DateTime.Now.Date}
12
},
// Default value of the Parameter
13
new
RouteValueDictionary {
14
{
"year"
,
@"(19|20)d{2}"
},
15
{
"month"
,
@"d{1,2}"
},
16
{
"date"
,
@"d{1,2}"
}
17
}
// Parameter rules. Here we limit the year, month, and day in the url to the data format we want.
18
);
19
20
}
21
22