There are a number of instances in which you may need to handle file requests from your web applications in a custom fashion. HTTP Handlers provide us with a very easy to use means by which to extend our web applications to use ASP.NET to handle custom file types.
Click here to download the sample solution for this tutorial
Creating a Simple Handler
In ASP.NET, an HTTP Handler is a means by which to process a specific endpoint request. Specifically, an HTTP Handler is used to process specific file types from within your ASP.NET website or application. The .NET Framework gives you a tremendous amount of flexibility and power when creating custom processing for specific HTTP requests. In ASP.NET, an HTTP Handler is any component that implements the IHttpHandler interface. The contract of this interface defines two key members: the IsReusable property and the ProcessRequest method.
- IsReusable: Gets a value indicating whether another request can use the IHttpHandler instance
- ProcessRequest: Enables processing of custom HTTP requests. This is the heart of your custom HTTP Handler. The argument passed to this method is the HttpContext object of the current processing request.
Defining logic for these two members satisfies all that is necessary to create an HTTP Handler. When you have defined your handler, you will register the handler with the ASP.NET application using the <httpHandlers /> node of the Web.config. To register a handler, you must specify three attributes:
- verb: This is the HTTP verb(s) that your handler will be used for when processing requests. The options are GET and POST
- path: This is the path of the file to process. In this instance, any file with an extension of .simple.
- type: The fully qualified type name of your HTTP Handler. In our example, this is SimpleHandler
After you have set up your web application using the above settings, you have successfully created and registered a custom HTTP Handler.
Now that you have an understanding of how HTTP Handlers work, let's do something a little fancier with our handler.
Creating a More Complex Handler
In the previous example, we built an HTTP Handler that simply flushed text out to the browser. Although this may satisfy some requirements, most real world usage of HTTP Handlers will be more complex.
Since the HTTP Handler provides us with access to the current HttpContext, we have access to the current HttpResponse object as well. We can use this object to send specific file types to the client. We can also send entire files to the client with the addition of a single HTTP header. In our ComplexHandler example, we will configure the application to handle RSS requests to a blog, download Word documents and flush a custom file to the client, all from the same handler.
The RSS request queries my blog for the RSS feed. It then transforms that RSS feed using a custom XSL transformation to display on a page. This process could very easily be modified to use a custom RSS source or generate only the RSS rather than a transformation of the RSS.
As you can see, HTTP Handlers bring the power of ASP.NET processing to custom file types in an extremely useful way.