ASP.NET ASHX File and HTTP Handler
A file with the ASHX file extension is an ASP.NET Web Handler file. A handler is responsible for fulfilling requests from a browser. ASHX files are used as supporting files in ASP.NET programming and can be opened with any program that codes in ASP.NET. These generic handlers have an extension of ASHX like "filename.ashx". Requests that a browser manages are either handled by file extension or by calling the handler directly. Only one handler can be called per request. A handler does not have any HTML static text like .aspx or .ascx files. A handler is a class that implements the IHttpHandler interface. If you need to interact with any Session information, you will also need to implement IRequiresSessionState. If you want to make an asynchronus handler, you will need to implement the IHttpAsyncHandler interface instead of the IHttpHandler interface.
HttpHandler is any class that implements the System.Web.IHttpHandler interface. so where are they used? Well, everywhere. Any time a request is made to the ASP.NET worker process, the request is passed on to the HttpHandler that has been configured to handle such requests. For example, when you go to http://localhost/default.aspx, IIS sees this request as one that should be handled by ASP.NET and hands it off to the worker process. Then, by looking at the extension, ASP.NET knows to process the request using System.Web.UI.PageHandlerFactory. Similarly, if a request is made to retrieve the web.config file, ASP.NET knows to process the page using System.Web.HttpForbiddenHandler.
<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory" /> <add verb="*" path="*.config" type="System.Web.HttpForbiddenHandler" /> <add verb="*" path="*.asmx" type="System.Web.Services.Protocols.WebServiceHandlerFactory" />
Above 3 recognizable extensions are provided by default in ASP.NET.
Create a Handler
Use Visual Studio to Create a new Web Site.

This will create a web site with an App_Data directory,the default.aspx and the web.config files. We won't use the default.aspx. Add a handler file by clicking on the website in the Solution Explorer and add new items "Generic Handler".
The default handler code file will look like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebSite
{
///
///
public class SimpleHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Run the application and set the path to SimpleHandler.ashx. You should get a response that looks like:

The SimpleHandler.ashx file implements the IHttpHandler interface and has two methods of "ProcessRequest" and "IsReusable". The ProcessRequest is our main method where we put our code. The IsReusable method can be set to true for another request can use the IHttpHandler instance. If the instance can be reused mark it true and this will improve the speed of the handler, and reduce the work our server will have to do. But if our instance should not be reused, due to state or because it is ansychronous,we should set IsReusable to false.
If you need read-only access to the Session, implement the IReadOnlySessionState interface otherwise IRequiresSessionState interface. Both interfaces are in the System.Web.SessionState namespace which we will need to add with the 'using' syntax. The code should now look like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace WebSite
{
///
///
public class SimpleHandler : IHttpHandler, IReadOnlySessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
It is often desirable to map an older URL or path to your new ASHX file. For backwards compatibility and for search engine optimization, you will probably want the new handler to take over an old URL in your site. To do this, use urlMappings which goes under>System.Web> in the Web.Config file.
<urlMappings enabled="true">
<add url="~/Default.aspx" mappedUrl="~/SimpleHandler.ashx"/>
</urlMappings>
An HTTPHandler does really is take an HTTPContext object and work out what to do with it.The <httpHandlers> configuration section handler is responsible for mapping incoming URLs to the IHttpHandler or IHttpHandlerFactory class. It can be declared at the computer, site, or application level.
<httpHandlers> <add verb="GET" path="CheckMyImageHandler.ashx" type="MyNamespace.AppsUI.Web.Fund.CheckMyImageHandler, MyNamespace.AppsUI.Web" /> <add verb="GET" path="DownloadMyImageHandler.ashx" type="MyNamespace.AppsUI.Web.Fund.DownloadMyImageHandler, MyNamespace.AppsUI.Web" /> </httpHandlers>
Administrators use the <add> tag directive to configure the <httpHandlers> section. <Add> directives are interpreted and processed in a top-down sequential order. Use the following syntax for the <httpHandler> section handler.
<httpHandlers> <add verb="[verb list]" path="[path/wildcard]" type="[COM+ Class], [Assembly]" validate="[true/false]" /> <remove verb="[verb list]" path="[path/wildcard]" /> <clear /> </httpHandlers>
The HttpHandler has only one method, one property and no events. Handlers are better for binary data, dynamic image view like check images in online banking, performance critical web pages and getting xml or rss feeds. ProcessRequest() will be invoked whenever the SimpleHandler.ashx file is requested. In a nutshell, An ASP.NET HTTP Handler is a simple class that allows us to process a request and return a response to the browser. The handler gives us access to the current web context and the session state.
Reference Articles
