Roll Your Own HttpHandler
page 3 of 7
by Brendan Enrick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 34213/ 91

Implementing the IHttpHandler Interface

Implementing interfaces is quite easy. If you are completely new to interfaces and do not understand them at all. I recommend you take a look at an article I wrote which describes the basics of how to use interfaces.

The IHttpHandler interface is a small one, containing only two methods. One is a property which is likely to have a set value. The other is the method which process the request. It is aptly named ProcessRequest. It takes as a parameter an HttpContext variable which is basically a one-stop shop object for dealing with requests. It gives you access to necessary objects and information.

The IReusable property does exactly as its name states. It determines for this class whether an instance of the class can be reused. If this value is set to true then the same class will continue to be used for subsequent requests. This means you could keep a count inside the class of the number of requests. You could have class-level variables handling the requests. This also means that the state of the class will change from request to request, which can cause problems if not handled correctly. The easiest answer is just to say that a class is not reusable unless you actually need it to be.

The last and most important function involved in HttpHandlers is the ProcessRequest method. As I stated earlier, it has one parameter of type HttpContext. This parameter gives access to the Response and Request objects which are two of the most commonly used objects in ASP.NET, and as such should be reasonably well understood. Request gives all of the information about the request which has been made, and Response is all of the information about the response which will be sent back to the browser. The context will also give access to the Sever and Application objects and plenty more.

In this example I explain how all of this works and just use a simple example to illustrate what I mean. I leave creating a more advanced solution up to future articles I write, as well as an exercise for the reader. I will name the handler SimpleHandler and set it to implement IHttpHandler. This is what the class looks like at the beginning.

Listing 1: SimpleHandler Empty

namespace RollYourOwnHttpHandler
{
    public class SimpleHandler : IHttpHandler
    {
        public SimpleHandler()
        {
        }
    }
}

Right now we have a compiler error, so we need to implement the interface we are saying we will implement. If we right click on the interface name and choose the "Implement Interface" option, it will add this code for us.

Listing 2: Interface Implementation

#region IHttpHandler Members
 
public bool IsReusable
{
    get { throw new NotImplementedException(); }
}
 
public void ProcessRequest(HttpContext context)
{
    throw new NotImplementedException();
}
 
#endregion

This code is just enough to get our class compiling. It will not work yet. First we need to remove those NotImplementedExceptions it is throwing. We can start with the IReusable property since it is by far the easier of the two.

In our example we will not be storing any class level information, so the state of our handler instance will never change. This means we should be relatively safe using the same instance of this class to handle multiple requests. This will make things nice for our solution as long as we maintain this lack of state. So for now we can just set the IsReusable property to true. Just so it is clear, I am changing that property to the following code.

Listing 3: IsReusable Property

public bool IsReusable
{
  get { return true; }
}

This leaves us with only one section of code left to write: the ProcessRequest method. In this section you can make your code as simple or as complicated as you like. Perhaps you will just be keeping a count of something. Maybe you will be making a record in a database which is logging this transaction. Or you could be doing some complex calculations about something. Either way this is the core of the handler. This is the handling code. Do in here whatever you wish. Keep in mind that if you are doing anything which affects the state of this instance of this class you will want to pay careful attention to that state as well as the IsReusable property.

Since this is just an example showing how to perform this action, it will just be a simple hello world program. To do this we can use the context object to give us access to the Response object. We can then just write some html directly into the Response. This is certainly not how you would want to use any complicated handlers, but it works well enough for our example.

Listing 4: ProcessRequest Method

public void ProcessRequest(HttpContext context)
{
    context.Response.Write("<html><head><title>Hi there!</title></head>" + 
        "<body>Hello world!</body></html>");
}

Notice that there are plenty of uses for HttpHandler. You could do many different tasks with them. This simple method is just to illustrate how to implement this method. You can certainly write plenty of other code here.


View Entire Article

User Comments

Title: Roll Your Own HttpHandler   
Name: namyaf
Date: 2009-07-31 1:47:12 PM
Comment:
This is the second article that I read by you, and both are explained very well without assuming too much from the reader. Great job and I look forward to more of your articles! peace yo!
Title: Roll Your Own HttpHandler   
Name: Ryan A
Date: 2008-07-16 9:49:57 AM
Comment:
Good article with quick, easy to follow examples. Thanks for sharing.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-10-12 11:25:45 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search