MyDevBank

MyDevBank

summaries and tech views for new developers C#,asp.net for web\mobile development,databases,iis6 and more.
Creating this site was an idea that popped up after some programing years, when you have to get back to the same things you all ready forgot, while new stuff is arriving to town. In this site you can find summaries on tech stuff im into, so for a stable wide base knowledge pyramid ,enjoy!!

Http Handlers

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 via the page handler.

The ASP.NET page handler is only one type of handler.
ASP.NET comes with several other built-in handlers such as the Web service handler for .asmx files.
You can create custom HTTP handlers when you want special handling that you can identify using file name extensions in your application. For example, the following scenarios would be good uses of custom HTTP handlers:

* RSS feeds To create an RSS feed for a site, you can create a handler that emits RSS-formatted XML. You can then bind the .rss extension (for example) in your application to the custom handler. When users send a request to your site that ends in .rss, ASP.NET will call your handler to process the request. *

Creating a Custom HTTP Handler

To create a custom HTTP handler, you create a class that implements the IHttpHandler interface to create a synchronous handler or the IHttpAsyncHandler to create an asynchronous handler. Both handler interfaces require you to implement the IsReusable property and the ProcessRequest method. The IsReusable property specifies whether the IHttpHandlerFactory object (the object that actually calls the appropriate handler) can place your handlers in a pool and reuse them to increase performance, or whether it must create new instances every time the handler is needed.
The ProcessRequest method is responsible for actually processing the individual HTTP requests.
IHttpAsyncHandler is better used for long running operations like loading images or for some relatively processor intensive work.

Creating a File Name Extension

When you create a class file as your HTTP handler, you can have your handler respond to any file name extension that is not already mapped in IIS and in ASP.NET. For example, if you are creating an HTTP handler for generating an RSS feed, you can map your handler to the extension .rss.

Code:

public class MyHendlers:IHttpHandler
{
   public MyHendlers(){ }
   IHttpHandler Members bool IHttpHandler.IsReusable { get { return false; } }
   void ProcessRequest(HttpContext context)
   {
   System.Drawing.Image img = System.Drawing.Image.FromStream(File.Open(HttpContext.Current.Server.MapPath(context.Request.Url.AbsolutePath), FileMode.Open, FileAccess.Read));
   System.Drawing.Image result = img.GetThumbnailImage(150, 150, null, IntPtr.Zero);
   context.Request.ContentType = "image/jpeg"; result.Save(context.Response.OutputStream , ImageFormat.Jpeg);
  }
}


add to web.config

<httpHandlers>
   <add verb="GET,POST" path="*.jpg" type="MyHendlers"/> <- MyHendlers = the class name
</httpHandlers>

Click for IHttpAsyncHandler example
Comment | Related Tags

HOME| IIS6 pipeline| ASP.NET pipeline| Http Modules| Http Handlers| Caching| Web Services| Page Life Cycle| Ajax| Jquery| Session State
ViewState| SEO| Mobile Basics| Mobile Handsets| C# Modifiers| C# Keywords| N-Tier| Design patterns| Stack & Heap| WCF
Databases| Relational Model| functionalities| Stored procedures| fishmarket
MyDevBank
© 2010 All Rights Reserved