HTTP Handlers in ASP
HTTP Handlers in ASP
HTTP Handlers in ASP
NET
The low level Request and Response API to service incoming Http requests are Http Handlers in Asp.Net. 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.
The configuration section handler is responsible for mapping incoming URLs to the IHttpHandler or
IHttpHandlerFactory class. It can be declared at the computer, site, or application level. Subdirectories inherit
these settings. Administrators use the tag directive to configure the section. directives are interpreted and
processed in a top-down sequential order. Use the following syntax for the section handler:
<httpHandlers>
<add verb="[verb list]" path="[path/wildcard]" type="[COM+ Class], [Assembly]"
validate="[true/false]" />
<remove verb="[verb list]" path="[path/wildcard]" />
<clear />
</httpHandlers>
To create an HTTP handler, you must implement the IHttpHandler interface. The IHttpHandler interface has
one method and one property with the following signatures:
By customizing http handlers, new functionalities can be added to Web Server. Files with new extensions like
.text for a text file can be handled by Web Server by using http handlers. The future of customization can lead
to hosting .jsp pages in IIS by finding adequate ISAPI extensions. The following steps are involved to create
customized http handler:
using System;
using System.Web;
using System.Web.SessionState;
namespace ExampleHandler
{
public class Handlerclass : IHttpHandler
{
public Handlerclass()
{
}
#region Implementation of IHttpHandler
public void ProcessRequest(System.Web.HttpContext context)
{
HttpResponse objResponse = context.Response ;
HttpSessionState objSession = context.Session ;
objResponse.Write("<html><body><h1>Hello World from Handler") ;
objResponse.Write("</body></html>") ;
}
Step 2
Register this handler by adding the following text in the web.config file:
<httpHandlers>
<add verb="*" path="*.text" type="ExampleHandler.Handlerclass, ExampleHandler "/>
</httpHandlers>
Step 3
Go to Internet Information Services and select Default Web Site. Right Click and Select Properties. Select
Home Directory and click on Configuration. The Following Screen will appear:
Click on Add and give executable path and new extension and click OK.
Close IIS and Run TestApp website by using the URL: http://localhost/Testapp/hello.text
HttpForbiddenHandler
The sensitive files can be protected by Http Forbidden Handler. The Database driven web sites using MS
Access, the .mdb file has to be protected. To protect the .mdb files, we must follow the two steps given below:
1. Map .mdb file in IIS 2. Register the file extension in web.config with HttpForbiddenHandler.
This walkthrough illustrates the basic functionality of a custom HTTP module. An HTTP module is called on every
request in response to the BeginRequest and EndRequest events. As a result, the module runs before and after
a request is processed. If the ASP.NET application is running under IIS 6.0, you can use HTTP modules to
customize requests for resources that are serviced by ASP.NET. This includes ASP.NET Web pages (.aspx files),
Web services (.asmx files), ASP.NET handlers (.ashx files), and any file types that you have mapped to ASP.NET.
If the ASP.NET application is running under IIS 7.0, you can use HTTP modules to customize requests for any
resources that are served by IIS. This includes not just ASP.NET resources, but HTML files (.htm or .html files),
graphics files, and so on. For more information, see ASP.NET Application Life Cycle Overview for IIS 5.0 and
6.0 and ASP.NET Application Life Cycle Overview for IIS 7.0.The example module in this topic adds a message to
the requested ASP.NET Web page at the beginning of any HTTP request. It adds another message after the page
has been processed. The module includes code that makes sure that it does not add text to a request for any
other file type.Each event handler is written as a private method of the module. When the registered events are
raised, ASP.NET calls the appropriate handler in the module, which writes information to the ASP.NET Web page.
Prequisites
The walkthrough also assumes that you are working with IIS 6.0 or IIS 7.0. However, you can see the
functionality of the module even if you run the ASP.NET Development Server.
To begin, you will create a class file that implements the module.
using System;
using System.Web;
public class HelloWorldModule : IHttpModule
{
public HelloWorldModule()
{
}
public String ModuleName
{
get { return "HelloWorldModule"; } }
If the Web site does not build, correct any problems. The custom HTTP module must compile or you will
not be able to register the module.
Registering the HTTP Module in IIS 6.0 and IIS 7.0 Classic Mode
After you have created the HelloWorldModule class, you register the module by creating an entry in the
Web.config file. Registering the HTTP module enables it to subscribe to request-pipeline notifications.In IIS 7.0,
an application can run in either Classic or Integrated mode. In Classic mode, requests are processed basically
the same as they are in IIS 6.0. In Integrated mode, IIS 7.0 manages requests by using a pipeline that enables it
to share requests, modules, and other features with ASP.NET.The procedure for registering a module is different
in IIS 7.0 Classic mode and IIS 7.0 Integrated mode. This section describes the procedure for IIS 6.0 and IIS 7.0
Classic mode. The procedure for registering a module that is running in IIS 7.0 Integrated mode is described in
the next section.
To register the module for IIS 6.0 and IIS 7.0 running in Classic mode
1. If the Web site does not already have a Web.config file, create one under the root of the site.
2. Add the following highlighted code to the Web.config file:
<configuration>
<system.web>
<httpModules>
<add name="HelloWorldModule" type="HelloWorldModule"/>
</httpModules>
</system.web>
</configuration>
The code registers the module with the class name and the module name of HelloWorldModule.
The process for registering a module in IIS 7.0 Integrated mode is slightly different than the process for IIS 7.0
Classic mode.
1. If the Web site does not already have a Web.config file, create one under the root of the site.
2. Add the following highlighted code to the Web.config file:
<configuration>
<system.webServer>
<modules>
<add name="HelloWorldModule" type="HelloWorldModule"/>
</modules>
</system.webServer>
</configuration>
You can also register the module by using IIS Manager. For more information, see Configuring Modules in IIS 7.0.
The code registers the module with the class name and the module name of HelloWorldModule.
After you have created and registered your custom HTTP module, you can test it.
The HTTP module appends a string to the start and end of the response. The module automatically runs
during any request for a file whose extension is assigned to ASP.NET. For more information, see HTTP
Handlers and HTTP Modules Overview.