What Is An ASHX File Handler Or Web Handler

In ASP.NET, you probably spend most of your time creating .aspx files with .cs files as code behind or use .ascx files for your controls and .asmx files for web services.

A web handler file works just like an aspx file except you are one step back away from the messy browser level where HTML and C# mix. One reason you would write an .ashx file instead of an .aspx file is that your output is not going to a browser but to an xml-consuming client of some kind.
 
Working with .ashx keeps you away from all the browser technology you don't need in this case. Notice that you have to include the IsReusable property.
 

What does the code there do?

 
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.
  1. <%@ WebHandler Language="C#" Class="Handler" %>  
  2.   
  3. using System;  
  4. using System.Web;  
  5.   
  6. public class Handler : IHttpHandler  
  7. {  
  8.     public void ProcessRequest(HttpContext context)  
  9.     {  
  10.         context.Response.ContentType = "text/plain";  
  11.         context.Response.Write("Hello World");  
  12.     }  
  13.   
  14.     public bool IsReusable  
  15.     {  
  16.         get  
  17.         {  
  18.             return false;  
  19.         }  
  20.     }  
  21. }  

Using query strings

 
Developers commonly need to use the QueryString collection on the Request. You can use the Request.QueryString in the Handler just like you would on any ASPX web form page.
  1. <%@ WebHandler Language="C#" Class="QueryStringHandler" %>  
  2.   
  3. using System;  
  4. using System.Web;  
  5.   
  6. public class QueryStringHandler : IHttpHandler   
  7. {  
  8.     public void ProcessRequest (HttpContext context)   
  9.     {  
  10.         HttpResponse r = context.Response;  
  11.         r.ContentType = "image/png";  
  12.         string file = context.Request.QueryString["file"];  
  13.         if (file == "Arrow")  
  14.         {  
  15.             r.WriteFile("Arrow.gif");  
  16.         }  
  17.         else  
  18.         {  
  19.             r.WriteFile("Image.gif");  
  20.         }  
  21.     }  
  22.   
  23.     public bool IsReusable   
  24.     {  
  25.         get  
  26.         {  
  27.             return false;  
  28.         }  
  29.     }  
  30. }  
Now Debug the application,
 
When you pass Fille='Arrow' in query string like as follows
http://localhost:1372/FileHandler/QueryStringHandler.ashx?file=Arrow You will get the folowing output,

What Is An ASHX File Handler Or Web Handler
Otherwise you will get following output,

What Is An ASHX File Handler Or Web Handler
http://localhost:1372/FileHandler/QueryStringHandler.ashx?file=Image
 
The above code receives requests and then returns a different file based on the QueryString collection value. It will return one of two images from the two query strings.
 

When we use Handler

 
Here I want to propose some guidelines about when to use custom handlers and when to use ASPX web form pages.
 
Handlers are better for binary data, and web forms are best for rapid development.
 
Use web forms (ASPX) when you have,
  • Simple HTML pages
  • ASP.NET custom controls
  • Simple dynamic pages
Use handlers (ASHX) when you have,
  • Binary files
  • Dynamic image views
  • Performance-critical web pages
  • XML files
  • Minimal web pages


Similar Articles