Usage of HttpHandler
HttpHandler can be very handy in scenario when you don't require complete page cycle.
Some important point related to HttpHandler
1. HttpHandler can be used when we introduced new script language. New file extension can
be handled by separate handler.
2. We can enable session access in httphandler
3. Syncronous Handler,Asynchronous handler
4. Can be used for performance enhancement.
Web.config entry for each handler.
< httpHandlers >
< add verb="*" path="*.< file extension > " type="< HandlerName > "/ >
< /httpHandlers >
HttpHandler - Usage
Typical Usage could be logging,authenticatio,performance enhancement e.t.c. User can directly
call the handler (.ashx) file and perform some task withough going through page lifecycle.
Usage 1 - .ashx file
scenario 1 - if we only want to return some file or response string then do we need to go through
page cycle.
.ashx file
< %@ WebHandler Language="C#" CodeBehind="Usage1Handler.ashx.cs" Class="DS_Mukesh.Usage1Handler" % >
.ashx.cs file
namespace DS_Mukesh
{
using System;
using System.IO;
using System.Web;
public class Usage1Handler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
string fileName = "Usage1Handler";
Response.Clear();
Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
Response.ContentType = "text/plain";
Response.Write(htmlText);
Response.Flush();
Response.End();
}
public bool IsReusable
{
get { return true; }
}
}
}
Usage 2 - .cs file
Scenario 2 - Want to do some caching for some request having special file extension. It will save
data tranmission. following can be done for file caching as well.
public class WebHandler : IHttpHandler
{
public WebHandler()
{
}
public void ProcessRequest(System.Web.HttpContext context)
{
context.Response.Cache.SetExpires(DateTime.Now.Add(600));
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetValidUntilExpires(false);
< context.Response.writeFile(< some file name >);
}
public bool IsReusable
{
get
{
return true;
}
}
}
HttpHandler - Typical example
public class WebHandler : IHttpHandler
{
public WebHandler()
{
}
public void ProcessRequest(System.Web.HttpContext context)
{
HttpResponse httpResponse = context.Response ;
httpResponse.Write("Hello World ") ;
httpResponse.Write("") ;
}
public bool IsReusable
{
get
{
return true;
}
}
}
HttpHandler - Asynchronous
using System;
using System.Web;
using System.Threading;
class AsyncHandler : IHttpAsyncHandler
{
public bool IsReusable { get { return false; } }
public AsyncHandler()
{
}
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback callBack, Object extraData)
{
SomeWork asynch = new SomeWork(callBack, context, extraData);
asynch.StartAsyncWork();
return asynch;
}
public void EndProcessRequest(IAsyncResult result)
{
}
public void ProcessRequest(HttpContext context)
{
throw new InvalidOperationException();
}
}
class SomeWork : IAsyncResult
{
private bool _completed;
private Object _state;
private AsyncCallback _callback;
private HttpContext _context;
bool IAsyncResult.IsCompleted { get { return _completed; } }
WaitHandle IAsyncResult.AsyncWaitHandle { get { return null; } }
Object IAsyncResult.AsyncState { get { return _state; } }
bool IAsyncResult.CompletedSynchronously { get { return false; } }
public SomeWork(AsyncCallback callback, HttpContext context, Object state)
{
_callback = callback;
_context = context;
_state = state;
_completed = false;
}
public void StartAsyncWork()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncTask), null);
}
private void StartAsyncTask(Object workItemState)
{
_context.Response.Write("From Async Handler!");
_completed = true;
_callback(this);
}
}
Please see following link
http://msdn.microsoft.com/en-us/library/ms227433.aspx
HttpHandler - Synchronous
See below example
HttpHandler - SessionState(ReadOnly)
public class SessionReadOnlyHttpHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string Value = context.Session["SessionExample"] as String;
}
public bool IsReusable
{
get { return true; }
}
}
HttpHandler - SessionState(Read/Write)
public class SessionHttpHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string Value = context.Session["SessionExample"] as String;
Value = "Hello World";
context.Session["SessionExample"] = Value ;
}
public bool IsReusable
{
get { return true; }
}
}