Routing in ASP.NET4


Routing Allows us to build friendly URL's by decoupling the URL of the HTTP Request from the physical path of the web form that serves the Request. Previous versions of ASP.NET allows us to build freindly URLs but the Routing engine simplifies building friendly URL's. The routing engine is included in the System.Web.Routing assembly.

Suppose we have a WebForm named SoftwareProducts which is in the folder named Products .The classic approach to viewing the SoftwareProducts is to point the URL to the physical location of the SoftwareProducts WebForm. The URL will look something like /Products/SoftwareProducts.aspx?id=1.

Routing allows us to use the following URL for the above request /Products/Software/1 ,which is more easier for the user to understand.

In the following example we define routes for SoftwareProducts and ElectronicProducts WebForms.The RoutingPage WebForm will handle the common request. Following is the folder structure for the application

Routing1.gif


We write code that match URL patterns and the code that handles it ,in the application startup method Application_StartUp of global.asax .We use the new MapPageRoute method for this purpose. Also we need to import the new System.Web.Routing namespace which includes the RouteTable class.

protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.MapPageRoute(
             "Products",                       //Route Name
             " Products/Software/{ProductId}",//URL Pattern To Match
             "~/RoutingPage.aspx");          //Path To Redirect To The Page
        }

This route defines a URL pattern to match and the WebForm that will actually handle the request for that URL. All requests of the form Pages/Software/ProductId are handled by RoutingPage.aspx. ProductId here is the parameter in the URL. The value of ProductId is accessible to RoutingPage.aspx via the Page.RouteData[] indexer.

In the code of the RoutingPage WebForm we read the value of ProductId and then redirect to the appropriate page by using the following code.

    protected void Page_Load(object sender, EventArgs e)
    {
        //The following code extracts the value of ProductId  parameter
        string strProductId = Page.RouteData.Values["ProductId"].ToString();
        if (strProductId == "1")
        {
            Response.Redirect("..\\SoftwareProducts\\SoftwareProducts.aspx");
        }
        else if (strProductId == "2")
        {
            Response.Redirect("..\\ElectronicProducts\\ElectronicProducts.aspx");
        }

    }

Now if we add Products/Software/1 to the startup URL like :

http://localhost:1497/Products/Software/1 we will see the SofwareProducts page shown below.

Routing2.gif

We can also access the route parameters in the markup as

<asp:Label ID="Label1" runat="server" Text="<%$RouteValue: ProductId %>" />


The RouteValue expression makes it easier for us to use route data in markup, and avoids working with the more complex Page.RouteData in markup.

Up Next
    Ebook Download
    View all
    Learn
    View all