Before ASP.net 4.0, in order to make search engine-friendly and user-friendly URLs, we needed to implement Url rewriting ourselves. Now we don’t need to. .Net 4.0 does all this for us. UrlRouting is called Routing because it not only implements Url rewriting, but also can obtain the rewritten Url through parameters and use it on the page.
1. Common usage of Url Routing
UrlRouting is widely used in Asp.Net Mvc projects and is very useful in Mvc, so it is transplanted to webform. Let’s first look at how it is used in webform. Let’s assume a usage scenario: we need to create a page for daily blog posts. The url address we want is:
/archive/2010/05/10/default.aspx
This address will be mapped to the ~/posts.aspx file
To use UrlRouting, you need to register the UrlRouting rules into the RouteTable. The code for registering the Routing rules in the Global file is as follows:
view sourceprint?01 public static void RegisterRoutes(RouteCollection routes)
02 {
03 routes.Ignore("{resource}.axd/{*pathInfo}");
04
05 routes.MapPageRoute("blogs", //Give this UrlRouting rule a name
06 "archive/{year}/{month}/{date}/default.aspx", //Hope friendly Url address format
07 "~/blogs.aspx", //The mapped aspx page path
08 false, //Do you need to check user permissions?
09 new RouteValueDictionary{ { "year", DateTime.Now.Year },
10 { "month", DateTime.Now.Month },
11 {"date", DateTime.Now.Date}
12 },//Default value of parameter
13 new RouteValueDictionary {
14 {"year",@"(19|20)d{2}"},
15 {"month",@"d{1,2}"},
16 {"date",@"d{1,2}"}
17 } //Parameter rules, we limit the year, month and day in the url to the data format we want.
18);
19
20}
twenty one
22 void Application_Start(object sender, EventArgs e)
twenty three {
24 //Routing rules registered at Application_Start
25 RegisterRoutes(RouteTable.Routes);
26}
2. Use the UrlRouting parameter value in the page 1) Use the Route value in the background code
view sourceprint?1 protected void Page_Load(object sender, EventArgs e)
2 {
3 string year = (string)RouteData.Values["year"];
4 string month = (string)RouteData.Values["month"];
5 string date = (string)RouteData.Values["date"];
6}
2) Use on the page
view sourceprint?1 <asp:Literal ID="literalYear" runat="server" Text="<%$RouteValue:year %>"></asp:Literal>
2 -<asp:Literal ID="literal1" runat="server" Text="<%$RouteValue:month %>"></asp:Literal>
3 -<asp:Literal ID="literal2" runat="server" Text="<%$RouteValue:date %>"></asp:Literal>
3) Use RouteParameter in DataSource
view sourceprint?1 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDb %>"
2 SelectCommand="SELECT BlogID,BlogTitle FROM Blogs Where Category = @category">
3 <SelectParameters>
4 <asp:RouteParameter Name="category" RouteKey="category" />
5 </SelectParameters>
6 </asp:SqlDataSource>
4) Display RouteUrl on the page
view sourceprint?1 <a href='<%=GetRouteUrl("blogs",new {year=2010,month=05,date=05}) %>'>Blog on May 1, 2010</a>
3. The difference between UrlRouting and UrlRewrite
UrlRouting is a relatively new thing compared to Url rewriting. The advantage of UrlRouting is that it can do two-way conversion. It can not only do URL rewriting, but also obtain the rewritten Url address based on some parameters. However, it also has its own shortcomings. For example, if you want to rewrite the domain name, such as the blog address yukaizhao.cnblogs.com, UrlRouting cannot do it, and you can only use UrlRewrite.
http://www.cnblogs.com/yukaizhao/archive/2010/05/20/urlrouting_asp_net_40.html