Adding an HTTP Handler to manage Images requests in .Net

Building the Handler

First, make the handler just creating a new class, make it use the IHttpHandler interface to make it “interfaceable“. To make it part of the HttpHandler interface you need 2 methods: IsReusable and ProcessRequest.

The method IsReusable is used to determine if the handler should be kept in memory and be reused by any request or should make a new instance for every request. In our case our handler should be reusable so it handles every request saving proc time and memory.

The method ProcessRequest will get the request and send a response back, obviously.

public class UserImagesHandler : IHttpHandler
{
    public UserImagesHandler() { }
    public bool IsReusable { get { return true; } }

    public void ProcessRequest(HttpContext context) { 
        HttpRequest request = context.Request;
        HttpResponse response = context.Response;
        String file = System.IO.Path.GetFileName(request.Path);

        context.Response.ContentType = "image/" + System.IO.Path.GetExtension(file).Replace(".", "");
        if (request.RawUrl.ToLower().Contains("profile")) {
            response.TransmitFile(MyNameSpace.Config.PATH_IMG_USER_PROFILE + file);
        } else if (request.RawUrl.ToLower().Contains("login")) {
            response.TransmitFile(MyNameSpace.Config.PATH_IMG_USER_LOGIN + file);
        } else {
            response.StatusCode = 404;
            response.Write("Not found.");
        }

    }
}

Configuring Handler in Web.config

Now, I need to say IIS to send these requests to this handler. I can do that using the IIS configuration, but that’s limited to the file extension (you can only make a handler for each file extension, you can still create a new file extension for your handler but.. that wouldn’t fit here) or you can set it at the webconfig which gives you more flexibility.

There are two ways for adding this to the webconfig depending on if you are using IIS6 or IIS7, since you may have IIS6 at local and 7 at production or viceversa you may want to use both ways so it will work at any IIS.

First way, in IIS6, it is set under the <system.web> tag:

	<system.web>
		<httpHandlers>
            <add verb="*" path="imgs/users/*/*" type="UserImagesHandler" />
		</httpHandlers>
	</system.web>

Second, for IIS7, under the <system.WebServer> tag:

	<system.webServer>
		<handlers>
            <add verb="*" path="imgs/users/*/*" type="UserImagesHandler" name="UserImagesHandler" />
		</handlers>
	</system.webServer>

Notice that if you are using a namespace for the Handler class you will need to use it when setting it in the web.config tags.

Once that’s done your handler should work and the requests for the images in the virtual path will be catched and responsed with what we have at the real folder. We can also add other features like access control since this is a full request and we have access to the session, cookies or auth. cookies. We could also add querystrings to resize the responsed images, make any log or any other thing you want to control.

One thought on “Adding an HTTP Handler to manage Images requests in .Net

Leave a Reply

Close Bitnami banner
Bitnami