Channels ▼

Get a Handle on Custom HTTP Resources

HTTP handlers and modules are truly the building blocks of the ASP.NET web platform. While a request for a page or a web service is being processed, it passes through a pipeline of HTTP modules, such as the authentication module and the session module, and is then processed by a single HTTP handler. After the handler has processed the request, the request flows back through the pipeline of HTTP modules and is finally transformed into HTML code for the browser.

An HTTP handler is the component that actually takes care of serving the request. It is an instance of a class that implements the IHttpHandler interface. The ProcessRequest method of the interface is the central console that governs the processing of the request. For example, the Page class implements the IHttpHandler interface, and its ProcessRequest method is responsible for loading and saving the view state and for firing the events such as Init, Load, PreRender, and the like.

ASP.NET maps any incoming HTTP requests to a particular HTTP handler. A special breed of component—named the HTTP handler factory—provides the infrastructure for creating the physical instance of the handler to service the request. For example, the PageHandlerFactory class parses the source code of the requested .aspx resource and returns a compiled instance of the class that represents the page. An HTTP handler is designed to process one or more URL extensions. Handlers can be given an application or a machine scope, meaning that they can process the assigned extensions within the context of the current application or all applications installed on the machine.

If you want your application to do something “unusual” with an incoming HTTP request, then an HTTP handler, a HTTP module, or both are the tools it needs. As mentioned, an HTTP handler is a class that implements the IHttpHandler interface. The output for the request is built within the ProcessRequest method, as shown below:

using System.Web;
namespace HandlerExample 
    public class SimpleHttpHandler : IHttpHandler 
        // Override the ProcessRequest method
        public void ProcessRequest(HttpContext context) {
            context.Response.Write("<H1>Hello, I'm an HTTP handler</H1>");

        // Override the IsReusable property
        public bool IsReusable {
            get { return true; }

You need an entry point to be able to invoke the handler. An entry point is a public URL that is configured within IIS to handle requests directed at the URL. Let’s say that you want to handle a custom resource—SQLX resources. A SQLX resource is an XML file that contains a connection string and a command text.

<query connString="DATABASE=northwind;SERVER=localhost;UID=sa;">
    SELECT firstname, lastname FROM employees
    SELECT TOP 10 companyname FROM customers

The ProcessRequest method of the handler first extracts this information, then it proceeds with the execution of the query, and finally generates the output.

public class QueryHandler : IHttpHandler
    // Override the ProcessRequest method
    public void ProcessRequest(HttpContext context)
        // Parses the SQLX file 
        string connString = GetConnectionString(context);
        string queryCmd = GetQueryCommand(context);

        // Create the output as HTML
        string html = CreateOutput(connString, queryCmd);

        // Output the data

    // Override the IsReusable property
    public bool IsReusable
        get { return true; }


The ProcessRequest method receives the HTTP context of the request and from it retrieves the name of the file behind the requested URL.

string _file = context.Server.MapPath(context.Request.Path.ToString());

Once the physical server file to access is known, extracting the connection string and commands is child's play. Next, you run the query and format the output as HTML to server to the browser in an HTML page layout.

Having an HTTP handler in place doesn't automatically enable you to access SQLX resources over the Internet. Any HTTP request, in fact, is first handled by IIS, which then may redirect to ASP.NET and your handler. The key step to enable handling of SQLX files is to let IIS know about it. You need to add the SQLX extension to the IIS metabase and bind it to the ASP.NET ISAPI extension. To do so, you can copy the settings for .aspx resources.

Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.