Net 4.0 urlrouting implementation-friendly URLs

Source: Internet
Author: User
Tags date format datetime net string domain name

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



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.