ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    Security in .Net
    Web Services
    Windows Services
    Windows Controls
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    Windows Phone
    Ruby on Rails
    IIS 7
    Windows 8
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    Single Page App
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertise with Us
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> ASP.Net & Web Forms >> URL Routing in ASP.Net 3.5(IIS7)
URL Routing in ASP.Net 3.5(IIS7)
URL Routing in ASP.Net 3.5(IIS7)

by Haider M Rizvi on 11/3/2010 5:15:25 PM

Views: 23231       Comments: 13

URL Routing in ASP.Net 3.5(IIS7)

What Is Routing?

Service Pack 1 for the Microsoft .NET Framework 3.5 introduced a routing engine to the ASP.NET runtime. The routing engine can decouple the URL in an incoming HTTP request from the physical Web Form that responds to the request, allowing you to build friendly URLs for your Web applications.

Suppose you have an ASP.NET Web Form named CSharp.aspx, and this form is inside a folder named ‘Tutorial’. The classic approach to viewing a tutorial with this Web Form is to build a URL pointing to the physical location of the form and encode some data into the query string to tell the Web Form which author to display. The end of such a URL might look like the following: /Tutorial/CSharp.aspx?AuthorID=5, where the number 5 represents a primary key value in a database table full of authors.

Configuring ASP.NET for Routing

To configure an ASP.NET Web site or Web application for routing, you first need to add a reference to the System.Web.Routing assembly. The SP1 installation for the .NET Framework 3.5 will install this assembly into the global assembly cache, and you can find the assembly inside the standard "Add Reference" dialog box.

To run a Web site with routing in IIS 7.0, you need two entries in web.config. The first entry is the URL routing module configuration, which is found in the <modules> section of <system.webServer>. You also need an entry to handle requests for UrlRouting.axd in the <handlers> section of <system.webServer>.


<modules runAllManagedModulesForAllRequests="true">              


<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>


<!-- . . . -->   





<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>


<!-- . . . -->   





Once you've configured the URL routing module into the pipeline, it will wire itself to the PostResolveRequestCache and the PostMapRequestHandler events.

Configuring Routes

Now to configure route, the first thing is to register the route at application startup. To register the routes at application startup write the following code in your Global.asax file.

void Application_Start(object sender, EventArgs e)




private void RegisterRoutes()


RouteTable.Routes.Add("Tutorial",new("Tutorial/{subject}/{AuthorID}", new RouteHandler(string.Format("~/CSharp.aspx"))));


Here {subject}, {AuthorID} is name of Query Srting, through which we will get access to the value passed through query string.

Now, we need a RouteHandler .

public class RouteHandler : IRouteHandler


    string _virtualPath;

    public RouteHandler(string virtualPath)


         _virtualPath = virtualPath;


    public IHttpHandler GetHttpHandler(RequestContext requestContext)


        foreach (var value in requestContext.RouteData.Values)


            requestContext.HttpContext.Items[value.Key] = value.Value;


        return (Page)BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(Page));



Now, Routing is configured but what about the query string. How we can get access to values passed through query string. To get the data passed through query string we use context.Items[“ID”] instead of Request.QueryString[“ID”].

HttpContext context = HttpContext.Current;

String id = context.Items["AuthorID"].ToString();

Report Abuse Form

URL Routing in ASP.NET.
by Awadhendra Tiwari 9/14/2011 6:28:04 AM
Hi Haider,
Nice article, Great concept given . Its really helpful for beginners.

Report Abuse

URL Routing in ASP.NET.
by John Smith 9/14/2011 8:05:32 AM
Nice Article
Report Abuse

by dinesh singh 12/20/2011 3:18:11 AM
Nice Article
 How can we pass parameter in aspx page using response.redirect()
Report Abuse

URL Routing in ASP.NET.
by Jenry Hock 12/26/2011 1:33:50 AM
Great article, It solve my problem,
Thanks Haider.
Report Abuse

URL Routing
by James Smith 12/26/2011 4:47:52 AM
Thanks Haider,
Keep sharing such a useful article.
Report Abuse

URL Routing
by Arun Singh 12/26/2011 4:55:13 AM
Very nice post. Thanks for sharing with us.
Report Abuse

Using Response.Redirect() in URL Routing
by Haider M Rizvi 12/29/2011 1:31:16 AM

Hi Dinesh Singh,

Sorry for replying late.

As you can see in the Route, it is "Tutorial/{subject}/{AuthorID}", for "~/CSharp.aspx"

RouteTable.Routes.Add("Tutorial",new("Tutorial/{subject}/{AuthorID}", new RouteHandler(string.Format("~/CSharp.aspx"))));

The about Route means that if there is any URL like “~/Tutorial/Subject/AuthorID” then it will point to “~/CSharp.aspx”.

Suppose, you want to pass “Routing” as subject and “24” as AuthorID, then you can pass it like


No, if you want to access AuthorID use the cose below.

HttpContext context = HttpContext.Current;

String id = context.Items["AuthorID"].ToString();

Hope this will help you.

Report Abuse

Query String in URL Routing
by Boopathiraja K 3/29/2012 5:20:07 AM

Hi Haider,

I am using the URL Routing concept to design my URL.

It is working fine but for the following scenario:-

1. I am in the dashboard screen which has no query string parameters (http://localhost:4499/TestApp/Dashboard)
2. I navigate to settings page which has the query string parameters (http://localhost:4499/TestApp/Settings/100)
3. If I tried to navigate again to dashboard screen, the page is not routing properly (http://localhost:4499/TestApp/Settings/100/Dashboard)

I am using the folowing in my codes:-
routes.Add("Dashboard", new CustomRoute("TestApp/Dashboard", new URLRouteHandler("~/TestApp/Home.aspx")));
routes.Add("Settings", new CustomRoute("TestApp/Settings/{Id}", new URLRouteHandler("~/TestApp/Home.aspx")));

Can you help me to solve this issue?.

Also guide me how to use the below method:-

public CustomRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
            : base(url, defaults, routeHandler)

Report Abuse

Query String in URL Routing
by Haider M Rizvi 3/30/2012 7:03:21 AM
Hi Boopathiraja K,

Could you post code for all added routes.

Report Abuse

What is URL Redirection
by anil babu 7/30/2012 11:38:47 PM
what is url redirection?
My task is I am developing one URL like this:
That is my URL I am passing  Empno=1,I want to display that Empno=1 correspondind "Ename" in database table

Plz give me one simple example

How can i implement this concept

I am new this concepts

plz help me
Thank you,
Report Abuse

by anil babu 8/2/2012 9:39:24 PM
My url like this
I am passing one perameter like this ""

I want display URLName(Column Name) Value( display That URL in my browser using ASP.NET

Plz help me

Actual URL is:

I want RESULT:"\"

Final result,This value comming from DATABASE table
Report Abuse

How to integrate PAYPAL in ASP.NET?
by anil babu 8/16/2012 4:24:32 AM
How to integrate PAYPAL in ASP.NET?
give me one example
Report Abuse

Need help
by abed akhi 9/11/2012 10:55:26 AM
I am using your method but when I added the 
HttpContext context = HttpContext.Current;

String id = context.Items["AuthorID"].ToString();

to the load_page method I got problems where should I add this my code is:



 void Application_Start(object sender, EventArgs e)




    private void RegisterRoutes()


        System.Web.Routing.RouteTable.Routes.Add("Tutorial", new System.Web.Routing.Route(Server.MapPath("~/")+"webpages/Default5.aspx/{category}", new RouteHandler(string.Format("~/webpages/Default5.aspx"))));




 public String cat="fenous";

    protected void Page_Load(object sender, EventArgs e)



        HttpContext context = HttpContext.Current;

        cat = context.Items["category"].ToString();




    void Load_ListView()




            OleDbConnection MyCon;   // create connection

            OleDbDataAdapter com;  // create command

            // OleDbDataReader dr;  //Dataread for read data from database

            string datapath = "websitedatabase.mdb";

            string path = Server.MapPath("~/") + datapath;

            MyCon = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source=" + path + "; Jet OLEDB:Database Password=pass");

            com = new OleDbDataAdapter("Select * from images where Category='"+cat+"'", MyCon);

            MyCon.Open();                                        // open the connection

            DataSet ds = new DataSet();


            ListView1.DataSource = ds;




        catch (Exception ex)




and I've added the needed code in web.config and I've added the RouteHandler class so what's the problem what I need is that I have a webpage with the name : Default5.aspx I need to display different categories in this page depending on the category added in the url ...

Report Abuse
Title :
Comment :
Text ColorBackground Color
Ordered ListBulleted List
Horizontal Rule
Design ModeDesign
View HtmlHtml
Latest Article by Haider M RizviRSS Feed
Latest BlogsRSS Feed
Top Viewed ArticlesRSS Feed
Top Viewed BlogsRSS Feed
Latest Interview QuestionsRSS Feed
Total Online Users: 4856
MindStick DataConver
Advertise with Us
Copyright © 2014MindStick. All Rights Reserved.