Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Extending ASP.NET with HttpHandlers and HttpModules  : Page 3

Write custom file handlers and filters with ease in ASP.NET that perform special request-handling, or alter requests either before or after they're processed by the ASP.NET engine.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

I used VB.NET for the sample code, but you can use the .NET language of your choice. First, launch VS.NET, create an empty project (MyHttpHandler in the sample code), and add a new class to it. Add a reference to the System.Web namespace and add an Imports System.Web statement at the top of the class file. Add an Implements statement to add the IHttpHandler interface. The following code shows the class with an empty interface implementation.

Imports System.Web Public Class ChartHandler Implements IHttpHandler Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable Get End Get End Property Public Sub ProcessRequest( _ ByVal context As System.Web.HttpContext) _ Implements _ System.Web.IHttpHandler.ProcessRequest End Sub End Class

This example simply outputs some text in response to a request. Add the following code to implement the ProcessRequest method :

Public Sub ProcessRequest( _ ByVal context As System.Web.HttpContext) _ Implements System.Web.IHttpHandler.ProcessRequest Context.Response.Write _ ("<html><body><h3>Output of Chart " & _ "Handler</h3></body></html>") End Sub

Note how you gain access to the Response object via the HttpContext parameter.

Next, implement the IsReusable property as follows:

Public ReadOnly Property IsReusable() _ As Boolean _ Implements IHttpHandler.IsReusable Get Return True End Get End Property

The property simply returns True. Compile the project. Next, you must configure it.

Configuring the HttpHandler in IIS
After you develop an HttpHandler or HttpModule you must configure IIS and ASP.NET for the new code to take effect. There are actually two steps involved in running a custom HttpHandler. First, you use the IIS Application Configuration dialog to map the file extension to the ASP.NET engine. Second, you modify the configuration sections in the application's web.config file to specify the namespace and class you want to use to handle that extension.

For IIS, you add the required file extension in the Application Configuration dialog as shown in Figure 1. You can add or remove handlers using this dialog.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date