URL Routing with ASP.NET 4 Web Forms (VS 2010 and .NET 4.0 Series)
page 4 of 8
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 32945/ 141

Mapping URLs using ASP.NET Web Forms

ASP.NET 4.0 now allows you to also use the URL Routing engine to map URLs to ASP.NET Web Forms pages as well as ASP.NET MVC Controllers. 

Below is an example of how you can use the new MapPageRoute() helper method in ASP.NET 4.0 to map the /products/software URL to a “Products.aspx” page that lives immediately under the application root directory:

Figure 2

The first two parameters to the MapPageRoute() helper are the same as in MapRoute(). The first parameter provides a friendly name for the route, and the second specifies the URL format to match.  The third parameter, though, points to a Products.aspx page to handle the URL instead of a controller class.  You can optionally specify additional parameters to MapPageRoute() that take advantage of features like “route constraints” and provide “default values for parameters” just like you can with ASP.NET MVC based route registrations.

Within the Products.aspx page you can then write code like below that uses the new Page.RouteData property in ASP.NET 4.0 to retrieve the “category” parameter value mapped using the /products/{category} URL filter, and then databind the category products to display them:

Figure 3

In addition to programmatically accessing incoming route parameters using code like above, you can also take advantage of the new declarative <asp:routeparameter> control with any ASP.NET DataSource control to declaratively bind a value from a route as well.  For example, below we are using a <asp:routeparameter> statement to bind the select statement’s @category parameter from the /products/{category} parameter in the URL route:

Figure 4

step4


View Entire Article

User Comments

Title: What about ASP.NET 3.5   
Name: Nicachipal
Date: 2011-02-08 11:08:29 AM
Comment:
Do we have something similar in ASP.NET 3.5 ?
Title: introduction to asp.net 4.0   
Name: Abdul Sami
Date: 2010-01-06 4:13:44 AM
Comment:
See if this article can help you people on asp.net 4.0 features

http://www.codeproject.com/KB/aspnet/Whatis_New_ASP_Net_4.aspx

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-05-22 11:32:41 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search