ASP.NET 4 URL Routing : A Quick Overview

Those who are currently building ASP.NET MVC web applications are very well aware of the Routing concept. URL routing enables the developer to make URLs flexible, dynamic and readable.
URL Routing was originally devised for ASP.NET MVC and has been implemented as a core feature. A route is a URL pattern which has a mapping with a handler, that can be physical file like the ASP.NET Web form .aspx.

URL routing in ASP.NET MVC and ASP.NET Web Forms differs on how you declare the final destination of the request. In ASP.NET MVC, you use a controller-action pair whereas in ASP.NET Web Forms, you use an ASPX path. In ASP.NET 4.0, URL pattern can be defined by defining its mapping with the physical .aspx file. In this pattern, a placeholder can also be defined so that variable data or parameter can be passed to the request handler without using a query string.

For defining routes in ASP.NET you need to use the following:
  • Route class: It provides properties and methods for the definition of route as well as for accessing the information of routes. This class defines the specification for the routing process in ASP.NET. Route object needs to be created to define mapping for every URL pattern that needs to map with the physical file or a class handle used to handle the request.
  • Routes: This is the property of the Route class. This represent the URL pattern which is used for processing incoming requests.
  • RouteTable: This class is used to store the URL routes defined for the application.
Let’s see a simple example of implementing Routing in ASP.NET

Step 1: Create an ASP.NET 4.0 Web Site (or Web Application), name it as ‘ASPNET_Routing’. In the Global.asax import the ‘System.Web.Routing’ namespace as below:

<%@ Import Namespace="System.Web.Routing" %>

Step 2: In the Application_Start write the following code:

ASP.NET Route Definition

The above code defines the routing table where the “DepartmentwiseEmployees.aspx” physical file is mapped to the URL route pattern ‘Department{DeptName}’. Here {DeptName} is the variable passed while making the request.

Step 3: In the project add a WebForm with name ‘DepartmentwiseEmployee.aspx’. Design it as below:

Web Form

Step 4: Write the following code in the Loaded event of the WebForm.

image

The first line in the Loaded event is as below:

string deptName = (string)Page.RouteData.Values["DeptName"];

The ‘RouteData’ property of the Page class gets the route data value (in this case DeptName) of the current request context. Here this value is always a string based upon which the filtering takes place and the result will be displayed.

Step 5: Run the website and the default page will be displayed http://localhost:1091/ASPNET_Routing. In the address bar type the following URL:

http://localhost:1091/ASPNET_Routing/Department/HRD

The result will be as shown below:

clip_image002

Download the source code





About The Author

Mahesh Sabnis is a Microsoft MVP having over 18 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). He also blogs regularly at DotNetCurry.com. Follow him on twitter @maheshdotnet

3 comments:

Fabian Fernandez said...

Nice article, easy to understand.

Timm Krause said...

@Fabian Fernandez /signed

Thanks.

Anonymous said...

Really helpful, but can we use regular expression in the example above as we use with URL Intelligencia.

Thanks