Friday, October 4, 2013

URL Routing in ASP.Net Web Forms Part - 2

In my previous post, I introduced the concept of URL Routing in ASP.Net. Now, let's look at the practical implementation for the same.

In a Web Forms application, you need to define routes in the Global.asax file. Following example shows how the routes are defined in the Global.asax file:

// Create a Method to register routes and call that methos in Application_Start event of Global.asax file.

public static void RegisterRoutes(RouteCollection routeCollection)
        {
            routeCollection.MapPageRoute("ProductDetail", "product/{productname}", "~/productdetail.aspx");
            routeCollection.MapPageRoute("ProductList", "products/{categoryname}/{subcategoryname}", "~/products.aspx", true, new RouteValueDictionary { { "categoryname", "Books" }, { "subcategoryname", 

"Biography" } });
        }

void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }

In the above code 2 routes are defined in the routeCollection object using MapPageRoute Method. Generally MapPageRoute takes 3 parameters (routeName,routeUrl,physicalFile). Alternatively, if we want to provide default values to the parameters of RouteURL, you can pass a RouteValueDictionary which is used to assign default values to the URL parameters. In the above example, the second route has URL set as products/{categoryname}/{subcategoryname} and the default values for {categoryname} and {subcategoryname} are set using RouteValueDictionary.

RegisterRoutes() method need to be called in the Application_Start event, so that routes get registered when on the application start.

Generally, using querystring the above thing is implemented as mentioned below:

productdetail.aspx?productname=5 point someone
OR
products.aspx?categoryname=Books&subcategoryname=Biography

To Send request to the Routed page you can use Response.Redirect or Response.RedirectToRoute.

In the above example, you need send request as given below:

Response.RedirectToRoute("ProductDetail", new { productname = "5 point someone" });
Response.RedirectToRoute("ProductList", new { categoryname = "Books", subcategoryname = "Biography" });

You can also use the Response.Redirect:

Response.Redirect("product/5 point someone");
Response.Redirect("products/Books/Biography");

But, the preferrable method is Response.RedirectToRoute().

For getting the parameter values passed on the destination page you need to use Page.RouteData.Values[""]. In the above example, values will be retrieved as given below:

string strProductName = Page.RouteData.Values["productname"].ToString();

string strCategoryname = Page.RouteData.Values["categoryname"].ToString();
string strSubCategoryname = Page.RouteData.Values["subcategoryname"].ToString();

For more details you can check following links of MSDN:
http://msdn.microsoft.com/en-us/library/cc668177.ASPX
http://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.mappageroute.ASPX

I hope you found this article useful. Please feel free to give your inputs in the comments section.

Thanks & Regards,
Munjal

No comments:

Post a Comment