User Id :    Password :      New Member   Forgot Password  
Http Handle
Description The HttpHandler are used by ASP.NET to handle requests.   No. of Views     1057
  Rating     0
Author Gaurav   Posted On     23 Aug 2010
Tags ASP.NET    

Sample Code   Download Code

Http Handle

Why would you want to create your own handler? For the most part, you would not. However sometimes it’s convenient to use a lower level interface that still provides access to useful objects such as response and request but doesn’t use the full control-based web form model. One example is if you want to create a web resource that dynamically renders a custom graphic. In this situation, you simply need to receive a request, check the URL parameters, and then return raw image data as a JPEG or GIF file. By avoiding the full web control model, you save some overhead, because ASP.NET does not need to go through as many steps (such as creating the web page objects, persisting view state, and so on.

Here is a simple tutorial for using an ASHX generic handler in ASP.NET, with code written in the C# programming language.

Getting started

This part lists the steps you can take to add a new ASHX file. To do this, open your ASP.NET web site. Go to the Website menu and click on the very first menu item there, "Add New Item". This will present the Add New Item dialog box. Select the "Generic Handler" item, and you will get a new file with some code in it called Handler.ashx.

Here we note what the autogenerated code in the ASHX file does. It defines two parts of the IHttpHandler interface. The important part is ProcessRequest(), which will be invoked whenever the Handler.ashx file is requested or pointed to. You shouldn't modify the interface inheritance or remove either of the members.

Part of Web.config file (XML) ---

    <urlMappings enabled="true">
        <add url="~/Default.aspx" mappedUrl="~/Handler.ashx"/>



URL mappings

The above Web.config markup will automatically link one URL to another. Now, when the Default.aspx page is requested, your Handler.ashx file will take over. This means you can map the default page in a directory to your handler.

Adding example image

By using ASHX file we will try to see our favorite image which you can take from your disk or on the internet and add it to your website project. In this example I choose “shahid2.jpg”. And next we will use this image in the Handler.ashx file.

Modifying Handler.ashx

Handler has two parts, and here we must modify the ProcessRequest() method. We can change the ContentType of the file and the Response content. Modify your Handler.ashx to be similar to the following, with your image ContentType and file name.

~~~ ASHX code-behind file (C#) ~~~

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        // Comment out these lines first:
        // context.Response.ContentType = "text/plain";
        // context.Response.Write("Hello World");

        context.Response.ContentType = "image/jpg";
        context.Response.WriteFile("~/shahid2.jpg ");

    public bool IsReusable {
        get {
            return false;

Testing handler

Here we test the new configuration and ASHX file on the local machine. Now click the green arrow to run your website on the development server. You should see the image in your browser. This is the result of writing the image to the response in the handler.


About Author

About Author I am as simple as c#. Gaurav
No Photo
Country India
Company N/A
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  


Write your comment here.
Verification Code