Wednesday, June 8, 2011

URL Rewriting in ASP.NET 4.0

URL Rewriting.

1. What is URL Rewriting?

What we do in normal scenerio is, we send some information to page in query string like:
http://www.productsite.com/productdetail.aspx?productid=10001
With the help to "URL Routing" new feature in ASP.NET 4.0 we can write hassel free URL rewriting code very easily. In 4.0 there is no need to write any handle aur module for Rewriting.

By using URL Rewriting we can convert above url to like
http://www.productsite.com/productdetail/10001
In this URL we are passing "10001" information to product detail page with the help of Routing.

The following table shows valid route patterns.

{controller}/{action}/{id}                       /Products/view/10001
{table}/Details.aspx                                /Products/Details.aspx
product/{action}/{entry}                        /product/show/123
{reporttype}/{year}/{month}/{day}       /sales/2008/1/5
{locale}/{action}                                   /US/show
{language}-{country}/{action}              /en-US/show


2. How to use Routing in ASP.NET Form:

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

public static void RegisterRoutes(RouteCollection routes)
{
     routes.MapPageRoute("",
     "Product/{action}/{productid}",
     "~/productdetail.aspx");
}
MapPageRoute method that take 3 arguments (sting,string,string) is a method of RouteCollection() class. MapPageRoute method create an object to Route and add it to RouteCollection().


3. How to set Default Value:

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

public static void RegisterRoutes(RouteCollection routes)
{
     routes.MapPageRoute("",
     "Product/{action}/{productid}",
     "~/productdetail.aspx",
     true,
     new RouteValueDictionary
     {{"productid", "10001"}, {"action", "view"}});
}

MapPageRoute(String, String, String, Boolean, RouteValueDictionary) methos that take 5 arguments.


4. How to set contraints in Routing:

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

public static void RegisterRoutes(RouteCollection routes)
{
     routes.MapPageRoute("",
     "Product/{action}/{productid}",
     "~/productdetail.aspx",
     true,
     new RouteValueDictionary
    {{"productid", "10001"}, {"action", "view"}},
    new RouteValueDictionary
    {{"productid", "[0-9]{5}"}});
}

If your URL doesn't match the patter then Route will not handle this request.

There are two ways to define constraints:
1. Regular expressions (While checking Regular expression it calls IsMatch method of Regex, and it will treat expression as case sensitive.)

2. Objects implement the IRouteConstraint interface (It calls Match method of IRouteConstraint interface to validate and Match method will return bool variable as a result to indicate wheather the object is valid or not)


5. How to access query string value on page

    Response.Write(Page.RouteData.Values["productid"]);
6. Key classes that ASP.NET 4.0 use for Routing:

  • Route
  • DynamicDataRoute
  • RouteBase
  • RouteTable
  • RouteCollection
  • RouteCollectionExtensions
  • RouteData
  • RequestContext
  • StopRoutingHandler
  • PageRouteHandler
  • RouteValueDictionary
  • VirtualPathData

No comments:

Post a Comment