Hi All,
Today i'll explain how to write our own handler for a new kind of page. Firstly, Handlers(as the term explain) are for handling things. These things are client requests. So for a client request to Default.aspx there will be a handler. This will be a default handler which will ship along with asp.net runtime. If you have new kind of extension you can writer your own handler.
The reason of writing this blog is for a good understanding of http handlers. I insist you to follow these steps and create your own handler. I have followed these procedures from the site:
http://www.15seconds.com/Issue/020417.htm
Step 1:
In u r VS, create a new c# class library and the name the class as MyOwnHandler.
The code should be
using System;
using System.Web;
using System.Text;
namespace MyCustomizedHandler
{
public class MyOwnHandler: IHttpHandler
{
public MyOwnHandler()
{
}
#region for implementing HttpHandler
Public void ProcessRequest(HttpContext context)
{
HttpResponse responseObject = context.Response;
responseObject.Write("<html><body><h1>My Customized page</h1>");
responseObject.Write("</body><html>");
}
Public bool IsReusable
{
get
{
return true;
}
}
#endregion
}
}
}
Rather than copying and pasting i insist all of u to type this small piece of code manually. Now just compile this from ur VS and paste the output of this dll into a virtual folder webapplication. I have a website in my iis with the name 'jsonSamples', so i placed the dll output of the above code into the bin directory of 'jsonSamples'.
Step2:
Now open the web.config file of the webapplication(in my case 'jsonSamples') and add the following piece of code inside it:
<add verb="*" path=".sample" type="MyCustomizedHandler.MyOwnHandler, MyCustomizedHandler">
</httpHandlers>
Step3:
All what we have done is in the application level. i.e., our application knows how to render a request for the page type .sample. Now we have to tell this to the iis webserver also. For that we have to do:
Launch IIS, right click on Default Web Site, go to home directories tab, click on configuration. This will open a new window. Now click on the add button and in the Executable section give the url of the aspnet_isapi.dll. Down to it, the extension should be given as 'sample'.
Click all ok's and close the window.
Now from ur browser just make a request some thing like this:
http://localhost/jsonSamples/helllo.sample
http://locahost/jsonSamples/239kshrkweh.sample
http://localhost/jsonSamples/anything.sample
You should get the output. (But i didn't get the output for the first time)
If u dont get the output then dont panic, just open ur webapplication(jsonSamples) in VS and for it add reference of u r class library dll. Even though u have placed the dll in the bin directory of the webappliation but u need to do add reference to the same item once again from VS. I dont know why but it works.
Hope this helped.