web analytics

Understanding HTTP Handlers and HTTP Modules in ASP.NET

Options

codeling 1602 - 6666
@2016-04-15 16:14:41

Any request comes from clients, first hits the kernel level HTTP.SYS of IIS.  HTTP.SYS and WAS Interacts each others and pass the request to proper Application Pool. Then Worker Process takes care of each and individual request.  Once the request done with HTTP Pipeline Processing, request enters into Page Lifecycle  state. 

image

As illustrated in the above figure, HTTP modules and HTTP handlers are an integral part of the ASP.NET architecture. While a request is being processed, each request is processed by multiple HTTP modules (for example, the authentication module and the session module) and is then processed by a single HTTP handler. After the handler has processed the request, the request flows back through the HTTP modules.

HTTP Module

An HTTP module is an assembly that is called on every request that is made to your application. HTTP modules are called as part of the ASP.NET request pipeline and have access to life-cycle events throughout the request. HTTP modules let you examine incoming and outgoing requests and take action based on the request.

Typical uses for HTTP modules include the following:

  • Security: Because you can examine incoming requests, an HTTP module can perform custom authentication or other security checks before the requested page, XML Web service, or handler is called. In Internet Information Services (IIS) 7.0 running in Integrated mode, you can extend forms authentication to all content types in an application.

  • Statistics and logging: Because HTTP modules are called on every request, you can gather request statistics and log information in a centralized module, instead of in individual pages.

  • Custom headers or footers: Because you can modify the outgoing response, you can insert content such as custom header information into every page or XML Web service response.

HTTP Handler

An ASP.NET HTTP handler is the process (frequently referred to as the "endpoint") that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler. You can create your own HTTP handlers that render custom output to the browser.

Typical uses for custom HTTP handlers include the following:

  • RSS feeds: To create an RSS feed for a Web site, you can create a handler that emits RSS-formatted XML. You can then bind a file name extension such as .rss to the custom handler. When users send a request to your site that ends in .rss, ASP.NET calls your handler to process the request.

  • Image server: If you want a Web application to serve images in a variety of sizes, you can write a custom handler to resize images and then send them to the user as the handler's response.

@2016-04-15 16:16:20

Built-in HTTP Handlers in ASP.NET

ASP.NET maps HTTP requests to HTTP handlers based on a file name extension. Each HTTP handler can process individual HTTP URLs or groups of URL extensions in an application. ASP.NET includes several built-in HTTP handlers, as listed in the following table.

 

Handler

Description

ASP.NET page handler (*.aspx)

The default HTTP handler for all ASP.NET pages.

Web service handler (*.asmx)

The default HTTP handler for Web service pages created as .asmx files in ASP.NET.

Generic Web handler (*.ashx)

The default HTTP handler for all Web handlers that do not have a UI and that include the @ WebHandler directive.

Trace handler (trace.axd)

A handler that displays current page trace information.

The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page handler.

The ASP.NET page handler is only one type of handler. ASP.NET includes several other built-in handlers such as the Web service handler for .asmx files.

@2016-06-30 01:13:21

ASP.NET HTTPHandlers are responsible for intercepting requests made to your ASP.NET web application server. They run as processes in response to a request made to the ASP.NET Site. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler.

ASP.NET offers a few default HTTP handlers:

  • Page Handler (.aspx): handles Web pages
  • User Control Handler (.ascx): handles Web user control pages
  • Web Service Handler (.asmx): handles Web service pages
  • Trace Handler (trace.axd): handles trace functionality

You can create your own custom HTTP handlers that render custom output to the browser. Typical scenarios for HTTP Handlers in ASP.NET are for example

  • delivery of dynamically created images (charts for example) or resized pictures.
  • RSS feeds which emit RSS-formated XML

You implement the IHttpHandler interface to create a synchronous handler and the IHttpAsyncHandler interface to create an asynchronous handler. The interfaces require you to implement the ProcessRequest method and the IsReusable property.

The ProcessRequest method handles the actual processing for requests made, while the Boolean IsReusable property specifies whether your handler can be pooled for reuse (to increase performance) or whether a new handler is required for each request.

 

Kerry

 

@2016-12-13 10:25:42

Generic Web handler (*.ashx)

Handlers are used to process individual endpoint requests. Handlers enable the ASP.NET framework to process individual HTTP URLs or groups of URL extensions within an application. Unlike modules, only one handler is used to process a request. All handlers implement the IHttpHandler interface, which is located in the System.Web namespace.

A Generic Handler is like an ASP.NET page that contains a single method that renders content to the browser. You can't add any controls declaratively to a Generic Handler. A Generic Handler doesn't support events such as the Page Load or Page PreRender events.

The .ashx file extension is usually reserved for custom handlers. Custom handlers created with the extension of .ashx gets automatically registered within IIS and ASP.NET.

The interface IhttpHandler requires the implementation of the following.

1) void ProcessRequest(HttpContext) : This method handles the actual processing for requests made.

2) bool IsReusable {get;} : The IsReusable() indicates whether or not your handler instance can be re-used for subsequent requests.  In some cases, after processing the request your handler may be in an incorrect state to process another request, especially if you have stored data about the previous request in member variables.  Note that the runtime will never use the same instance of your handler to process two requests at the same time, even if its marked as reusable.  If your handler does not store any per-request state in member variables and can have its ProcessRequest function called as many times as needed, make this property return true to allow reuse.

<%@ WebHandler Language="C#" %>

using System.Web;

public class CustomFormHandler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

 

 

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com