URL routing in ASP.NET 4 Web Forms

Original author: Scott Gu
  • Transfer
imageWhat is URL routing?

We introduced URL routing for the first time in ASP.NET 3.5 SP1, which is now used in ASP.NET MVC applications to provide clean, SEO-friendly, "web 2.0" URL addresses. URL routing allows you to configure the application to receive requested addresses that do not correspond to physical files. You can use routing to advertise URLs that are semantically meaningful to users and that can help search engine optimization (SEO).

For example, here is the URL address of a traditional page that displays product categories:

Using URL routing in ASP.NET 4, you can configure your application to accept the following URL to display the same information:

With ASP.NET 4.0, URLs as shown above can be bound to ASP.NET MVC controller classes and to simple ASP.NET Web Forms pages.

Binding URLs using ASP.NET MVC

Routing URL engine introduced with ASP.NET 3.5. SP1 provides a powerful way to intercept incoming URLs. You write code that is part of the application download for registering / binding URLs that match a specific format for writing handlers.

The following is an example of how you can use ASP.NET MVC today to bind / products / software URLs to the “Products” controller class, which has a method called “Browse”:


Binding URLs using ASP.NET Web Forms

ASP.NET 4.0 also allows you to use the routing URL engine to bind URL addresses to ASP.NET Web Forms pages.

The following is an example of how you can use the MapPageRoute () helper method in ASP.NET 4.0 to bind the / products / software URL to the “Products.aspx” page, which is located directly in the root directory:


The first two parameters of the MapPageRoute () helper are exactly the same as MapRoute (). The first parameter specifies a friendly name for the route, the second defines the format of the URL address. The third parameter tells Products.aspx to process the URL instead of the controller class. You can optionally define additional parameters for MapPageRoute (), which are used as “route restrictions” and provide “default values ​​for parameters”, as is possible with ASP.NET MVC when registering a route.

On the Products.aspx page, you can write code, as shown below, that uses the new ASP.NET 4.0 property Page.RouteData to get the value of the “category” parameter bound using the URL filter / products / {category}, which connects the product category to display :


In addition, for programmatic access to the incoming parameters of the router using the above code, you can use the new declarative control with any ASP.NET DataSource control for declaratively associating a route value. For example, below, we useto associate the @category select request parameter with the / products / {category} parameter in the router URL:


Retrieve URL addresses in ASP.NET Web Form

The ASP.NET URL routing engine can use in both cases: link the incoming URL to the handlers in the code and for programmatically generating outgoing URLs using the same binding registration logic.

For example, above, when we linked the / product / {category} URL, we gave it a friendly name - “products-browse”. This allows us to use the new helper method Page.GetRouteUrl () to search for a route in the routing system of URL addresses, optionally determine the parameters for it and then return the real URLs.

For example, the code below will return the URL “/ products / software”


You can access the helper method in the code-behind file or directly in the .aspx file.

Also, a set of Response.RedirectToRoute () methods has appeared, which can be used to redirect users along the route (no matter where to use it in MVC or Web Forms) and optionally pass parameters to it.

PostBack scripting

URL routing in ASP.NET 4.0 fully supports postback scripts. Control
will automatically substitute the URL at which the page was generated. For example, if you open a page with the address / products / software, any server control will generate output - this means that any postback script that occurs on the page retains the original URL.

All this allows you to maintain clean, SEO-friendly, readable URLs in Web Forms and postback scripts, and avoids the ingenious tricks that are now constantly used in URL rewrite modules to achieve some features.

Also popular now: