To create the custom HelloWorldHandler HTTP handler class
1.) In Solution Explorer, right-click the project, click Add ASP.NET Folder, and then click App_Code.
2.) In the App_Code folder, create a class named HelloWorldHandler and add the following code to the class file.
using System.Web;
public class HelloWorldHandler : IHttpHandler
{
public HelloWorldHandler()
{
}
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
// This handler is called whenever a file ending
// in .sample is requested. A file with that extension
// does not need to exist.
Response.Write("<html>");
Response.Write("<body>");
Response.Write("<h1>Hello from a synchronous custom HTTP handler.</h1>");
Response.Write("</body>");
Response.Write("</html>");
}
public bool IsReusable
{
// To enable pooling, return true here.
// This keeps the handler in memory.
get { return false; }
}
}
Registering the Custom HTTP Handler in IIS 6.0
After you have created the custom HTTP handler class, you must register it in the application's Web.config file. This enables ASP.NET to find the handler when requests are made for resources whose URL ends with .sample.
There are different procedures for registering the handler, depending on whether you are working with IIS 6.0 or IIS 7.0. This section describes how to register a handler in IIS 6.0. The next section describes how to register a handler in IIS 7.0.
To register the handler in IIS 6.0
Add the following highlighted element to the Web.config file.
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="*.sample"
type="HelloWorldHandler"/>
</httpHandlers>
</system.web>
</configuration>
The configuration element registers the custom handler by class name, and it maps the .sample file name extension to that handler.
Register an application extension mapping for the .sample file name extension by using IIS Manager. For more information, see How to: Configure an HTTP Handler Extension in IIS.
Registering the Custom HTTP Handler in IIS 7.0
In IIS 7.0, an application can run in either Classic or Integrated mode. In Classic mode, requests are processed much the same way 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.
For IIS 7.0, the handler registration requires either registering the handler in the Web.config file or in IIS Manager. Because of the centralized administration in IIS 7.0, changes in an application's Web.config file are reflected in IIS Manager interface for the application and vice versa. In the following procedures, the handlers are registered in the Web.config file.
There are different procedures for registering the handler for IIS 7.0 running in Classic mode and running in Integrated mode. Follow the procedure for the IIS mode that you are using.
To register the handler in IIS 7.0 running in Classic mode
Add the following highlighted element to the Web.config file.
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="*.sample"
type="HelloWorldHandler"/>
</httpHandlers>
</system.web>
<system.webServer>
<handlers>
<add verb="*" path="*.sample"
name="HelloWorldHandler"
type="HelloWorldHandler"
modules="IsapiModule"/>
scriptProcessor="%path%\aspnet_isapi.dll"
</handlers>
</system.webServer>
</configuration>
The configuration element registers the custom handler by class name and maps the .sample file name extension to that handler.
Because you are registering a custom file name extension, you register the handler in both the handlers section and the httpHandlers section. In Classic mode, for backward compatibility, the handler is specified as an ISAPI module by using the modules attribute. The path of the ASP.NET ISAPI dll is specified by using the scriptProcessor attribute. The name attribute is required in the handlers section.
To register the handler in IIS 7.0 running in Integrated mode
Add the following highlighted element to the Web.config file.
<configuration>
<system.webServer>
<handlers>
<add verb="*" path="*.sample"
name="HelloWorldHandler"
type="HelloWorldHandler"/>
</handlers>
</system.webServer>
</configuration>
The configuration element registers the custom handler by class name and maps the .sample file name extension to that handler.
Testing the Custom HTTP Handler
After you have created and registered the custom HTTP handler, you can test it.
To test your custom HTTP handler
In the browser, request a page from the Web application.
In the browser, enter a URL that ends in .sample. For example, enter the following URL:
http://localhost/HttpHandler/test.sample
The text defined in the HelloWorldHandler class is displayed.
0 comments:
Post a Comment