URL Routing With ASP.NET 4 - Web Forms

26. June 2010 18:38

.NET Framework ASP.NET C#  8 Comments

URL Routing was first introduced in .NET framework 3.5 SP1 but MVC has built-in and works pretty decently to create SEO friendly URL and prevents URL hacking. ASP.NET 4.0 is now introduced with a new feature called URL routing with web forms. URL routing help developers to create short and friendly URLs which enhance search engine page ranking. There are few other ways to create short friendly URLs like URLrewriter.net extension or if you have a physical access to IIS you can have installed URL Rewriter extension for IIS 7 to create short friendly URLs. Hey! not everyone has access to IIS!! So if you don't have the access then also you can re-write the URLs using this new feature in ASP.NET 4.0. 

One thing I would like to mention is that when you create a new ASP.NET web application in Visual Studio 2010, it won't show up with a blank page, but instead build a full applicaton with sample pages with a pretty good design. As you see below I haven't design this page..actually this is a default template when you create a new ASP.NET web application. What I have done here is just put a text box to enter contact ID and a button to get the details from the Adventure Works sample database.


This is a pretty simple interface and now we take a look at some internal work of this web application. My primary focus is on having simple URLs for my application for better search engine optimizations. This application has two main pages apart from the about and other pages that added to the project through the template. The Default.aspx is the main page where we have a field which allow the user to enter the contact ID for the person he want to search. The other page which handles the request and show the details of the contact person is called View.aspx. But this is a really tricky part from a user's perspective as a user will never see this page on the browser address bar while navigating a website. Check the View.aspx design code and check the <asp:SqlDatasource> tags and notice the SelectParameter tag. As I am using a select query with a parameter to populate the grid, the SelectParameter tag further uses <asp:RouteParameter> with route name and route key.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>" 
        SelectCommand="select Title,FirstName,MiddleName,LastName, EmailAddress from Person.Contact where ContactID=@id">
            <asp:RouteParameter Name="id" RouteKey="id" />

OK! Let's start up what we have on the Default.aspx page. This is the default page and a user will see this page first. Write the below code on the button to redirect the request to route handler. Here I have used a Regex expression to validate if the user enters a numeric ID and not any alphanumeric or alphabet. This check is just a workaround, I recommend you to use a better validation technique.

if((Regex.IsMatch(txt_pid.Text.Trim(), @"\d+")) == true)
          new { id = txt_pid.Text });

So does this URL make any sense? Not at this moment but surely it will after if you have registered your routes in the Global.asax file. My Global.asax file has a method called void RegisterRoutes(RouteCollection Route)

void RegisterRoutes(RouteCollection routes)

The method MapPageRoute accepts some parameters. The first parameter - "Person-Details" you see is the name of the Route which can be any thing you like. The second parameter - "Person/{id}" is the URL which we have generated. In short this is the URL which is visible to the user and outside world, what is happening internally only a developer knows!. The third parameter - "~/View.aspx" is the physical file which actually process the request and return it to the second parameter. The second parameter is the route URL and you can name it what you like except the parameter you are passing, just make sure you use the same parameter name everywhere. In the method void RegisterRoutes(RouteCollection Route) you can register number of route handlers in a single go under void Application_Start method in the Global.asax file.

void Application_Start(object sender, EventArgs e){
// Code that runs on application startup

Once you registered the routes in the Global.asax file, you are done. Time to press F5 and see the action. On the default page enter the ID and hit the Get Details button. The page rendered in front of you / user will be having a clean tidy URL.


 Download: ASP.NET4URLRouting.zip (175.58 kb)

Currently rated 5.0 by 1 person

Comments (8)

Alex Alex
7/2/2010 8:10:12 PM #

Hi Prashant, If I understood you right, URL Routing is not going to work on ASP.NET 3.5? (my host doesn't want to offer .NET 4.0 yet unfortunately).

Prashant Prashant
7/3/2010 4:42:57 PM #

Hi Alex, You can use URL Routing with ASP.NET 3.5, but not like the way you use in ASP.NET 4.0. You first need to add a reference of System.Web.Routing and then you can use Routing in your ASP.NET 3.5 project. But the syntax will be not as easy as it is in 4.0.

Alex Alex
7/5/2010 5:20:03 PM #

OK, I understood, thank you very much!

Alta White Alta White
9/8/2010 10:40:28 AM #

Is URL Routing and Web.Routing is same thing or different in regards of redirection. ???

Prashant Prashant
9/19/2010 1:36:30 PM #

Web.Routing is the namespace which provides classes to URL Routing. If you look at the example, you will see that I have used [i]System.Web.Routing[/i] to implement URL Routing in my web application.

Aaron Crowder Aaron Crowder
7/13/2011 2:55:47 AM #

Thanks for the article! This is just what I was looking for today. Trying to spice up an older CMS application that I help maintain at my job with pretty URL's.

Ankit Singh Ankit Singh
1/10/2012 1:30:59 PM #

Hi, I was reading your article and I would like to appreciate you for making it very simple and understandable. This article gives me a basic idea of URL Routing in ASP.Net 3.5(IIS7) and it helped me a lot. Thanks for sharing with us. Check out this link too its also having nice post with wonderful explanation on URL Routing in ASP.Net 3.5(IIS7), for more details check this.... http://mindstick.com/Articles/9992a0bc-90f5-4f04-823a-31f901b61643/?URL%20Routing%20in%20ASP.Net%203.5%28IIS7%29 Thank you very much!

Roy Roy
9/16/2015 7:15:54 AM #

Thanks your post is very useful. I also refer very helpful and useful article about URL Routing in ASP.Net 3.5(IIS7) Please visit this helpful article http://www.mindstick.com/Articles/9992a0bc-90f5-4f04-823a-31f901b61643/URL%20Routing%20in%20ASP%20Net%203%205%20IIS7#.VfkUMpc0Xcc http://stackoverflow.com/questions/7371208/url-routing-is-not-working-in-iis-7-0-witrh-apllication-pool-asp-net-2-0-classic

Add Comment

Visit blogadda.com to discover Indian blogs Computers Blogs