Introduction to ASP.NET HTTP Modules and Handlers
All requests to IIS are handled through Internet Server Application Programming Interface (ISAPI) extensions. ASP.NET has its own filter to ensure pages are processed appropriately. By default, the ASP.NET ISAPI filter (aspnet_isapi.dll) only handles ASPX, ASMX, and all other non-display file formats used by .NET and Visual Studio. However, this filter can be registered with other extensions in order to handle requests to those file types, too, but that will be covered later.
Every request flows through a number of HTTP modules, which cover various areas of the application (i.e. authentication and session intofmation). After passing through each module, the request is assigned to a single HTTP handler, which determines how the system will respond to the request. Upon completion of the request handler, the response flows back through the HTTP modules to the user.
The HTTP Pipeline
To understand the role of HTTP modules within ASP.NET, you need to understand how the HTTP pipeline works. Once an HTTP request comes in over port 80 (the regular HTTP port or port 443 for HTTPS and the secure sockets layer), the request passes through a number of stages making up the HTTP pipeline before it’s actually fielded by your application.
Microsoft® Internet Information Services (IIS) is the first participant in the chain. IIS is still used to route the request to the ASP.NET runtime. IIS maps the ASP.NET file extensions to ASPNET_ISAPI.DLL, an ISAPI extension provided with ASP.NET. The job of ASPNET_ISAPI.DLL is to forward the request to the ASP.NET worker process, ASPNET_WP.EXE. From that point on, the request is wrapped up into an instance of HttpContext and piped through a number of ASP.NET classes. The HttpContext class includes members representing such things as the Response and the Request and also includes security information along with anything else you might want to know about the request.
The next step for the request is to pass through an instance of HttpApplication.After the HttpApplication object massages the request, it pushes the request through one or more HttpModule objects. There are a number of system-level HTTP modules.The final piece of the chain is the HttpHandler
HTTP Module
In order to insure that your HttpHandler is hooked up to the HTTP pipeline requires an entry in the application’s web config. or the machine.config file. The same holds true for HTTP modules. When an HTTP Module is added to the pipeline, the ASP.NET runtime will call the methods of the IHttpModule interface at the appropriate times. When the module is first created by the runtime, the Init method is called. As illustrated above, the parameter passed into the Init method allows your code to tie into the HttpApplication object.
The following table lists the key events exposed by the HttpApplication object. Note that all these events are implemented as multicast delegates so that numerous modules can register for each one.
Event
When It’s Called
BeginRequest
Before request processing starts
AuthenticateRequest
To authenticate client
AuthorizeRequest
To perform access check
ResolveRequestCache
To get response from cache
AcquireRequestState
To load session state
PreRequestHandlerExecute
Before request sent to handler
PostRequestHandlerExecute
After request sent to handler
ReleaseRequestState
To store session state
UpdateRequestCache
To update response cache
EndRequest
After processing ends
PreSendRequestHeaders
Before buffered response headers sent
PreSendRequestContent
Before buffered response body sent
In addition to the above methods, we can also respond to the methods in the global.asax file for the HttpApplication object. These events include the session start and end events, as well as the start and end events for the application.
HTTP Handlers
HTTP handlers are the .NET components that implement the System.Web.IHttpHandler interface. 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 difference 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.
HTTP modules are classes that have access to the incoming request. These modules can inspect the incoming request and make decisions that affect the internal flow of the request. After passing through the specified HTTP modules, the request reaches an HTTP handler, whose job it is to generate the output that will be sent back to the requesting browser. The following diagram illustrates the pipeline an ASP.NET request flows through.
Photo Resize Handler
In this article we’ll create a HTTP Handler in ASP.NET to resize an image as per parameters that user passes.
Creating the Handler
please go and find topic from that site http://www.dotnetic.com/dnet/?q=node/5
please use following code to PhotoResizeHandler.ashx.cs ( Append)
define a method for resize image in same file :
private static byte[] ResizeImageFile(byte[] imageFile, int height, int width)
{
using (Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
{
int newwidth = 0;
int newheight = 0;
float oldwidth = oldImage.Width;
float oldheight = oldImage.Height;
float imgratio = oldwidth / oldheight;
if (imgratio > 1)
{
newwidth = width;
newheight = Convert.ToInt32 ( height / imgratio);
}
else
{
newheight = height;
newwidth = Convert.ToInt32(width * imgratio);
}
Size newSize = new Size(newwidth, newheight);
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
{
using (Graphics canvas = Graphics.FromImage(newImage))
{
//canvas.SmoothingMode = SmoothingMode.AntiAlias;
//canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
//canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
MemoryStream m = new MemoryStream();
newImage.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
return m.GetBuffer();
}
}
}
}
Invoking Handler from aspx page
You would invoke the handler from aspx page just like you call any other aspx page.
Following Image control invokes the handler:
<asp:Image ID=”Image1″ ImageUrl=”~/PhotoResizeHandler.ashx?imageFile=images/Leaves.jpg&height=200&width=200″
runat=”server” />
or like this :
ImageButton lnkPhotoThumb = ( (ImageButton)( e.Item.FindControl( “PhotoThumb” ) ) );
lnkPhotoThumb.ImageUrl = Page.ResolveUrl( “~/Handlers/PhotoResizeHandler.ashx?imageFile=” + Page.ResolveUrl( “~/UploadedFiles/Resources/”)Leaves.jpg + “&height=120&width=120″ );
ref: http://www.dotnetic.com/dnet/?q=node/5
Also , a big thanks to Shelly (my colleague) who provide me the logic of maintaining the aspect ratio of images
Posted by nissankg