Asp net routing with web forms

ASP.NET is a popular programming used for web applications. One of the key features of ASP.NET is its routing capability, which allows to define custom URLs for their web forms. In this article, we will explore how to implement ASP.NET routing with web forms, along with some examples to illustrate the concepts.

Routing in ASP.NET allows you to map URLs to web forms or handlers, providing a and user-friendly URL structure for your application. Instead of having long and complex URLs with strings, you can have shorter and more meaningful URLs that are easier to remember and understand.

To get started with ASP.NET routing, you need to define the routes in your application. This can be done in the `Global.asax` file, which is the entry point for your application. In the `Application_Start` event handler, you can use the `RouteTable` class to define your routes.

Let's consider an example where we have a web form called `ProductDetails.aspx` that displays the details of a specific product. Instead of having a URL like ``, we can define a route that maps the URL `{productId}` to the `ProductDetails.aspx` web form.

To define this route, we can add the following code in the `Application_Start` event handler:

Example 1: Defining a Route

RouteTable.Routes.MapPageRoute("ProductDetails", "products/{productId}", "~/ProductDetails.aspx");

In the above code, we use the `MapPageRoute` method of the `RouteTable.Routes` collection to define the route. The first parameter is the name of the route, which can be used to generate URLs using the `GetRouteUrl` method. The second parameter is the URL , which can contain placeholders for route parameters. The third parameter is the virtual path of the web form that should be displayed when the route is matched.

Now, when a user visits the URL ``, the `ProductDetails.aspx` web form will be displayed, and the value `123` will be passed as the `productId` route parameter.

To access the route parameters in the web form, you can use the `RouteData` property of the `Page` class. The `RouteData` property provides access to the route data, including the route parameters.

Example 2: Accessing Route Parameters

protected void Page_Load(object sender, EventArgs e)
    string productId = Page.RouteData.["productId"] as string;
    // Use the productId to fetch the product details from the database

In the above code, we access the `productId` route parameter using the `Page.RouteData.Values` collection. We then use this value to fetch the corresponding product details from the database or perform any other necessary operations.

ASP.NET routing also supports constraints, which allow you to specify validation rules for route parameters. For example, you can specify that the `productId` route parameter should be a numeric value. If a user visits a URL like ``, which doesn't match the constraint, ASP.NET will automatically return a 404 error.

Example 3: Constraints

RouteTable.Routes.MapPageRoute("ProductDetails", "products/{productId}", "~/ProductDetails.aspx", null, new RouteValueDictionary { { "productId", @"d+" } });

In the above code, we pass a `RouteValueDictionary` object as the fifth parameter to the `MapPageRoute` method. This dictionary contains the constraints for the route parameters. In this case, we specify that the `productId` route parameter should match the regular `d+`, which represents one or more digits.

By using ASP.NET routing with web forms, you can create clean and SEO-friendly URLs for your application. It not only improves the user experience but also makes your application more maintainable and scalable. With the ability to define custom routes and access route parameters, you have full control over the URL structure of your web forms.

In conclusion, ASP.NET routing with web forms is a powerful feature that allows you to define custom URLs for your web applications. By following the examples provided in this article, you can easily implement routing in your ASP.NET web forms and create user-friendly URLs for your application.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents