URL Rewriting in ASP.NET Web Form



URL Rewriting-asp.net url rewriting has lots of benefits, listing its main benefits

1.     SEO Friendly URL
2.     Secured URL
3.     No need to change bookmark with change in site structure.

URL Rewriting Scenario

Here i am trying to describe the scenario where we use url rewriting.suppose we have a page called "DisplayProducts.aspx" that takes a category name as a querystring argument, and filters the products according that querystring value.  The corresponding URLs to this DisplayProducts.aspx  page look like this:

http://usetricks.com/DisplayProducts.aspx?CID=books
http://usetricks.com/DisplayProducts.aspx?CID=bikes
http://usetricks.com/DisplayProducts.aspx?CID=pens

Rather than use a querystring to expose each category, we want to modify the application so that each product category looks like a unique URL to a search engine, and has the category keyword embedded in the actual URL (and not as a querystring argument).


URL Rewriting Using Request.PathInfo Parameters Instead of QueryStrings

To understand this see the diffrence between below two urls.

http://usetricks.com/DisplayProducts.aspx?CID=books
and
http://usetricks.com/DisplayProducts.aspx/books

One thing you'll notice with the above URLs is that they no longer have Querystring values - instead the category parameter value is appended on to the URL as a trailing /param value after the DisplayProducts.aspx page handler name.Simply use the Request.PathInfo property, which will return the content immediately following the DisplayProducts.aspx  portion of the URL.


protected string Getcategory()
    {
        string CATNAME = "";
        if (Request.PathInfo.Length == 0)
        {
            CATNAME= "";
        }
        else
        {
            CATNAME= Request.PathInfo.Substring(1);
        }

        return CATNAME;
               
    }

 In this technique there is no server configuration changes are required in order to deploy an ASP.NET application.

Using HttpContext.RewritePath() to Perform URL Rewriting

This method allows a developer to dynamically rewrite the processing path of an incoming URL, and for ASP.NET to then continue executing the request using the newly re-written path.


For example, we could choose to expose the following URLs to the public:

http://usetricks.com/books.aspx
http://usetricks.com/pens.aspx
http://usetricks.com/bikes.aspx

This looks to the outside world like there are three separate pages on the site (and will look great to a search crawler).Now we have to use Application_BeginRequest event in Global.asax.


void Application_BeginRequest(object sender, EventArgs e)
    {

        string fullOrigionalpath = Request.Url.ToString();

        if (fullOrigionalpath.Contains("/Books.aspx"))
        {
            Context.RewritePath("/DisplayProducts.aspx?CID=books");
        }
        else if (fullOrigionalpath.Contains("/pens.aspx"))
        {
            Context.RewritePath("/DisplayProducts.aspx?CID=pens");
        }
    }



Tags: , , , , ,

Join Us!