Use of Asp.net4 practical method URL Routing

Source: Internet
Author: User

The most exciting of the web forms of the

Asp.net 4 is the full support of the url routing mechanism. In the past in Asp.net 3.5 sp1, web forms more or less began to support the url routing mechanism, which gives us more flexibility in the presentation and use of the Web site.  &NBSP
    In the past we asp.net, accustomed to the following Web site rendering way: Http://myWebSite/EditProduct.aspx? ID=1 &NBSP
    But the rise of restful web sites (or Rest web services) in recent years, you often see the way URLs are rendered: http://myWebSite/ PRODUCT/EDIT/XBOX &NBSP
    Web sites like these have some benefits, first, URLs can express more clearly what they want to render, or the behavior of the backend application; URLs such as these are not corresponding to the back-end of an entity file (. aspx.cs), but through the routing mechanism to transfer, so compared to the previous web site has a higher security.  &NBSP
    that is, under such a framework, ASP. NET application URL is no longer just a path to an entity file, but it can be used to express the functionality to be performed. As for the actual execution of the program code or the rendering results of the Web page, it is not necessarily the site to point to the entity page. This also allows us to develop applications with greater flexibility, such as the General blog site URL may be "http://blog/studyhost", since most blog sites can open to many people to apply for, of course, every user has a similar "http://blog/ Requester id "such a URL. Of course, the back end of the asp.net should be the same set of procedures to deal with, in the past ASP.net applications, we have to take a hard time to allow the site to accept such a Web site design (change the parameters from QueryString to rest style), and now, through the URL The  routing mechanism will soon be easy to handle.  &NBSP
    In addition, there is another obvious benefit, the rest-styleURL: http://myWebSite/Product/Edit/XBOX vs. http://myWebSite/EditProduct.aspx? ID=1 &NBSP
    compared to traditional Web sites more easily by Google and other search engines to query and retrieve, after all, the Xbox on the map site than the product id=1 easy to understand more. So how do we use such a mechanism in asp.net 4, you just need to use the Mappageroute method to easily specify the URL route through the newly added routes category, for example:   

View Plaincopy to Clipboardprint?
protected void Application_Start (object sender, EventArgs e) {
RouteTable.Routes.MapPageRoute (
"Testroute", "Search/{productname}", "~/webform1.aspx");
}
protected void Application_Start (object sender, EventArgs e) {
RouteTable.Routes.MapPageRoute (
"Testroute", "Search/{productname}", "~/webform1.aspx");
}



after Global.asax so writes, when the user types in the URL column: http://localhost:1031/Search/AK47


The Web page (application Master) will be directed to the WebForm1.aspx page, where the parameter ProductName "AK47" can be obtained in the following way:


View Plaincopy to Clipboardprint?
protected void Page_Load (object sender, EventArgs e) {
Response.Write ("Searching Product Name:" +
page.routedata.values["ProductName"]);
}
protected void Page_Load (object sender, EventArgs e) {
Response.Write ("Searching Product Name:" +
page.routedata.values["ProductName"]);
}



This way of design, it is easy to easily many, do not underestimate such a mechanism, which allows us to develop large Web applications become possible, in line with the ASP.net 4 of the DynamicData technology we want to introduce later, We have been able to easily develop a single. aspx Web page (a program) that maintains a variety of data tables with different backend schemas.  Unlike in the past ASP.net 2.0, if the back-end database has a lot of data tables to deal with, we almost have to create a separate. aspx maintenance page for each datasheet, even if the behavior on each. aspx Web page is almost exactly the same as the Code logic (CRUD).


more interestingly, with the universalization of the URL routing mechanism, even the familiar datasource controls have added a routeparameter to join the festivities, now ASP.net 4.0 Web forms can be said to be the URL routing machine The system fully supported:





view plaincopy to clipboardprint? 
<asp:linqdatasource id= "LinqDataSource1"  runat= "server"      
     ContextTypeName 
= "Urlrouting.dataclasses1datacontext"  EntityTypeName 
= ""   tablename= "Customers"  where= "companyname  
==  @CompanyName" >    
    <WhereParameters>   
      <asp:routeparameter name= "CompanyName"  RouteKey 
= "CompanyName"  type= "String"  />   
    </WhereParameters>   
</ asp:linqdatasource>   
<asp:linqdatasource id= "LinqDataSource1"  runat= " Server   
    ContextTypeName= 
Urlrouting.dataclasses1DataContext " EntityTypeName 
=" " tablename=" Customers " where=" companyname 
 ==  @CompanyName "> 
    <WhereParameters> 
      <asp:routeparameter name= "CompanyName"  RouteKey 
= "CompanyName"   Type= "String"  /> 
    </WhereParameters> 
</asp: linqdatasource> 



you will find that, through the syntax above, we can allow LinqDataSource to query the parameters of the where condition when they are queried, directly referencing the parameter values in the URL routing, similar to the querystringparameter of the past, Quite convenient and easy to use

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.