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