Friday, February 11, 2011

Handlers

Whenever Client do the request to the Server it always goes through the Modules and HttpHandlers.After the Handler it again pass through the modules. A request can go through multiple Modules at a time but pass through only one handler at a time.

If you want to modify request in the middle than you can use HTTP Handlers for that.

To service incoming Http requests Asp.Net use Http Handlers. All handlers implement the IHttpHandler interface, which is located in the System.Web namespace. Handlers are somewhat analogous to Internet Server Application Programming Interface (ISAPI) extensions.

Any class that implements the IHttpHandler interface can act as a target for the incoming HTTP requests. HTTP handlers are somewhat similar to ISAPI extensions. One similarity between HTTP handlers and ISAPI extensions is that HTTP handlers can be called directly by using their file name in the URL, similar to ISAPI extensions.

The following are the methods in IHttpHandler interface

ProcessRequest : Used to call Http Requests.
IsReusable : To check the re-usability of same instance handler with a new request of same type.

We can use <httpHandlers> tag for adding HTTP handlers to our Web applications like..

<httpHandlers>
<add verb="supported http verbs" path="path" type="namespace.classname, assemblyname" />
<httpHandlers>

Attributes

verb = The verb attribute defines allowed HTTP request methods for this handler.If the handler supports all of the HTTP verbs, simply use "*", otherwise list the supported verbs in a comma separated list. So if your handler supports only HTTP GET and POST, then verb attribute will be "GET,POST". Possible values are head, get, post etc*.

path = The path attribute specifies the path or wild card specification of the files for which this handler will be invoked. For example, if you want your handler to be called only when test.xyz file is requested, then the path attribute will contain "test.xyz"; similarly if you want your handler called for any file having .xyz extension, the path attribute will contain "*.xyz".

type =The type attribute specifies the actual type of the handler or handler factory in the form of a combination of namespace, class name and assembly name. ASP.NET runtime first searches the assembly DLL in the application's bin directory and then searches in the Global Assembly Cache (GAC). In type you can give your Handler name also if you have added it through ASP .Net new Items.

The <add> directives are processed in a top-down sequential order. If two or more <add> sub elements specify the same verb/path combination, the final <add> overrides all others.

When we use HttpHanlers?

If your ASP.NET or Sharepoint web application is storing binary data (images, PDF’s) in a database, And you want to pick those images and display to user as if it were normal files on the server (www.example.com/name1.jpg). A clever way to do that is using the IHttpHandler interface.

Even ASP.Net itself use Handlers for its internal processing. You can see in the Machine.config file
<httpHandlers>
<add verb="*" path="trace.axd" type="System.Web.Handlers.TraceHandler"/>

<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory"/>

<add verb="*" path="*.ashx" type="System.Web.UI.SimpleHandlerFactory"/>

<add verb="*" path="*.config" type="System.Web.HttpForbiddenHandler"/>

<add verb="GET,HEAD" path="*" type="System.Web.StaticFileHandler"/>

. . . . . .
. . . . . .
</httpHandlers>

No comments:

Followers

Link