before. NET 4.0, in order to make search engine friendly, user-friendly URLs are required to implement their own URL rewrite, now do not need,. NET 4.0 for us to do all this. Urlrouting is called routing because it implements URL rewriting and can be used on the page by parameter-rewritten URLs.
1. Common usage of URL Routing
Urlrouting is widely used in the ASP.net MVC project and works well in MVC, so porting to WebForm, we'll look at how we use it in WebForm.
Suppose a usage scenario: we need to do the page of the blog Daily article, we want the URL address 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 routetable, the code for registering the routing rule in the following global file
The following are the referenced contents: public static void RegisterRoutes (RouteCollection routes) { Routes. Ignore ("{resource}.axd/{*pathinfo}"); Routes. Mappageroute ("blogs",//give this urlrouting rule a name "Archive/{year}/{month}/{date}/default.aspx",//Hope friendly URL address format "~/blogs.aspx",//map to the ASPX page path False,//Whether you need to check user rights New routevaluedictionary{{"Year", DateTime.Now.Year}, {"Month", DateTime.Now.Month}, {"Date", DateTime.Now.Date} Default value for},//parameter New RouteValueDictionary { {"Year", @ "(1920) \d{2}"}, {"Month", @ "\d{1,2}"}, {' Date ', @ ' \d{1,2} '} }//Parameter rules, we are here to limit the date in the URL to the data format we want ); } void Application_Start (object sender, EventArgs e) { Routing rules registered at Application_Start RegisterRoutes (routetable.routes); } |
2. Use urlrouting parameter values in the page
1 using route values in the background code
The following are the referenced contents: protected void Page_Load (object sender, EventArgs e) { String year = (string) routedata.values[' year ']; String month = (string) routedata.values["Month"]; String date = (string) routedata.values["Date"]; } |
2 use on the page
The following are the referenced contents: <asp:literal id= "literalyear" runat= "Server" text= "<% $RouteValue: Year%>" ></asp:Literal> -<asp:literal id= "Literal1" runat= "Server" text= "<% $RouteValue: Month%>" ></asp:Literal> -<asp:literal id= "Literal2" runat= "Server" text= "<% $RouteValue:d ate%>" ></asp:Literal> |
3) Use of Routeparameter in DataSource
The following are the referenced contents: <asp:sqldatasource id= "SqlDataSource1" runat= "Server" connectionstring= "<%$ connectionstrings:testdb" Selectcommand= "SELECT blogid,blogtitle from Blogs Where Category = @category" > <SelectParameters> <asp:routeparameter name= "category" routekey= "category"/> </SelectParameters> </asp:SqlDataSource> |
4 display RouteUrl on the page
The following are the referenced contents: <a href= ' <%=getrouteurl ("blogs", new {year=2010,month=05,date=05})%> ' >2010 year May 1 blog </a> |
3. The difference between urlrouting and Urlrewrite
urlrouting relative to the URL rewrite is a relatively new thing, Urlrouting's advantage is that can do bidirectional conversion, can do URL rewrite, can also be based on some parameters to get rewritten URL address, but it also has its own deficiencies, such as if you want to even rewrite the domain name, such as blog address yukaizhao.cnblogs.com Such rewriting, urlrouting can not do, can only use Urlrewrite.
Original address: http://www.cnblogs.com/yukaizhao/archive/2010/05/20/urlrouting_asp_net_40.html