Skip navigation

An HTTP Request can be directly associated with an HTTP Handler or with an HTTP Handler factory object. An HTTP handler factory is a class that implements the IHttpHandlerFactory interface and is in charge of returning the actual HTTP handler to use to serve the request. In the .NET Framework, HTTP handler factories are used to perform some preliminary tasks on the requested resource prior to passing it on to the handler. The internal class namedPageHandlerFactory represents a typical handler factory object. It figures out the name of the handler to use and, if possible, loads it up from an existing assembly.

 

HTTP handler factories are classes that implement a couple of methods on the IHttpHandlerFactory interface-GetHandler and ReleaseHandler, as shown in the following table

 

Method

Method Description

GetHandler

Returns an instance of an HTTP handler to serve the request.

ReleaseHandler

Takes an existing HTTP handler instance and frees it up or pools it.

The GetHandler method has the following signature

public virtual IHttpHandler GetHandler(HttpContext context,  

                                       string requestType, 

                                       string url, 

                                       string pathTranslated);

The requestType argument is a string that evaluates to GET or POST-the HTTP verb of the request. The last two arguments represent the raw URL of the request and the physical path behind it. The ReleaseHandler method is a mandatory override for any class that implements IHttpHandlerFactory; in most cases, it will just have an empty body.

The following listing shows an example HTTP handler factory that returns different handlers based on the HTTP verb (GET or POST) used for the request:

 namespace HttpHandlerFactoryDemo

{

    class MyHandlerFactory : IHttpHandlerFactory

    {

        public IHttpHandler GetHandler(HttpContext context, 

            string requestType, String url, String pathTranslated)

        {

            if(context.Request.RequestType.ToLower() == "get") 

                return (IHttpHandler) new MyGetHandler();

            else if(context.Request.RequestType.ToLower() == "post")

                return (IHttpHandler) new MyPostHandler();

            return null;

        }

        public void ReleaseHandler(IHttpHandler handler)

        {

            // nothing to do

        }}}

 

When you use an HTTP handler factory, it's the factory, not the handler that needs to be registered with the ASP.NET configuration file. If you register the handler, it will always be used to serve requests. If you opt for a factory, you have a chance to decide dynamically and based on runtime conditions which handler is more appropriate for a certain request.

 

Happy Learning !!!

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish