Friday, 1 February 2013

Mapping a File Extension in IIS for HTTP Handler file with C# Examples and VB.Net Examples

Click to Download C# Example
Click to Download VB.NET Example

Http Handler using ".ashx" file extension and this is very convenient and understandable to all of us. But we can also custom this file extension with our requirement. To make customization in file extension we need to do add class file in "App_Code" folder and this class file implement "IHttpHandler" interface. And also mapping file extension in IIS and add settings in "web.config" file.

This configuration tell the application to show which file extension this handler serves. We can You do this by adding an <httpHandlers> section to the web.config file.
Here is configuration setting :
        <add verb="*" path="ImageHandler.img" type="ImageMappedHandler, App_Code"/>

This settings direct the application to use the "ImageMappedHandler" class to process incoming requests for ImageHandler.img. We can also specify wildcards for the path. By specifying *.img for the path which indicates that we want the application to use the ImageMappedHandler class to process any request with the
.img file extension. By specifying * indicates that we want all requests to the application to be processed using the handler.

Additional Configuration for IIS 7 :
If we run out web application on IIS 7 then we also must add the <handlers> configuration section to the <system.webServer> configuration section in our application’s config file. When adding the handler configuration in this section, we must also include the name attribute.

Configuration for IIS 7 :
      <add name="ImageHandler" verb="*" path="ImageHandler.img" type="ImageMappedHandler, App_Code" />

Now after doing this settings we can directly call ImageHandler.img from browser and we can get image from that.

Here is example for this.

In this example we add one "ImageMappedHandler" class file in "App_Code". This class implements "IHttpHandler" interface and we override "ProcessRequest" method. This methods serves image content base on query string value. Now we configure this handler in web.config file by adding "ImageHandler.img" as path.

This file does not exist physically but when this file called our handler's "ProcessRequest" call automatically and serves images base on query string value. We can use same handler file path to get various images.

ASPX Code :
            HTTP Handler class Impliment in Img tag
            Id : 1</h1>
        <img src="ImageHandler.img?id=1" alt="Dynamic Image" />
            Id : 2</h1>
        <img src="ImageHandler.img?id=2" alt="Dynamic Image" />

C#. Net Example (ImageMappedHandler.cs file) :
public class ImageMappedHandler : IHttpHandler

    public void ProcessRequest(HttpContext context)
        context.Response.ContentType = "image/jpeg";
        if (context.Request.QueryString["id"] == "1")

    public bool IsReusable
            return false;


VB.Net Examples (ImageMappedHandler.vb file) :
Public Class ImageMappedHandler : Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        'context.Response.ContentType = "text/plain"
        'context.Response.Write("Hello World")

        context.Response.ContentType = "image/jpeg"
        If context.Request.QueryString("id") = "1" Then
        End If

    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Return False
        End Get
    End Property

End Class

Output : 
Mapping a File Extension in IIS for HTTP Handler file
(To view original image , click on image)

You can also call that "ImageHandler.img" URL directly in browser.

Here is example for this.

Mapping a File Extension in IIS for HTTP Handler file
(To view original image , click on image)

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles according to that.

Below are the books that you would like :