Generic Web handler (*.ashx)
Handlers are used to process individual endpoint requests. Handlers enable the ASP.NET framework to process individual HTTP URLs or groups of URL extensions within an application. Unlike modules, only one handler is used to process a request. All handlers implement the IHttpHandler interface, which is located in the System.Web namespace.
A Generic Handler is like an ASP.NET page that contains a single method that renders content to the browser. You can't add any controls declaratively to a Generic Handler. A Generic Handler doesn't support events such as the Page Load or Page PreRender events.
The .ashx file extension is usually reserved for custom handlers. Custom handlers created with the extension of .ashx gets automatically registered within IIS and ASP.NET.
The interface IhttpHandler requires the implementation of the following.
1) void ProcessRequest(HttpContext) : This method handles the actual processing for requests made.
2) bool IsReusable {get;} : The IsReusable() indicates whether or not your handler instance can be re-used for subsequent requests. In some cases, after processing the request your handler may be in an incorrect state to process another request, especially if you have stored data about the previous request in member variables. Note that the runtime will never use the same instance of your handler to process two requests at the same time, even if its marked as reusable. If your handler does not store any per-request state in member variables and can have its ProcessRequest function called as many times as needed, make this property return true to allow reuse.
<%@ WebHandler Language="C#" %>
using System.Web;
public class CustomFormHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
public bool IsReusable {
get {
return false;
}
}
}