Tuesday, December 29, 2009

Various URL rewriting methods in ASP.Net C# Overview

When we have multiple parameters in our URL, at the time it is possible that hackers can identify our logic based on parameter and try to get into the system with those parameters. To resolve this issue we can use URL rewriting. URL rewriting is the process of hiding our parameterized URL and make it simple, browser and crawler friendly URL.
URL rewriting is also useful for Search Engine Optimization (SEO). When you use parameter on your single page and load different data. Search engine (i.e. Google) will consider it as single page. But when you use URL rewriting it will consider that all pages are different.

Ways of Programming

In .Net there are 3 ways to achieve URL Rewriting.
1.       With Http handler factory
2.       With Http Module
3.       Global.asax

URL Rewriting with Http Handler Factory


When you use handler based URL rewriting you can specify that for particular extension this handler will be called. So for each request your code will not execute. What handler factory does is select appropriate handler based on your code and returns it to compiler to handle the request.


When you want to handle directory without any page you need to specify wild card for the handler factory. In this case each request will be go through your code. And for image or static files it will fire error (if not handled) that cannot parse file. To avoid this issue you can specify static file handler in web.config file above the wild card (such as .jpg, .gif), so your file will be treated as static.

URL Rewriting with Http Module


When you are using this method you do not require to specify any extension or wild card in web.config. All file will be checked for rewrite and if any code is not available then it will load file as normal http request.


In this case request for each either a page or static file like images will pass through the module. So it will cause more processing. If the code is not proper then it will execute each of your rule/code for all requests.



This will work same as http module.


This method does have one more disadvantage over http module. In IIS 7 some of methods are restricted so you either need to recode or need to go over http module.