I was in a need to create HttpHandler. I also needed to use the session.
Here is my first attempt:
public class ChbHandler : IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
HttpRequest req = context.Request;
HttpResponse resp = context.Response;
resp.Write("HttpHandler: Hello World!");
}
#endregion
}
In Here if you try to use the Session object you will not it is null. Why is that?
I googled a bit and found that in order to support session, your HttpHandler should inherit the
IRequiresSessionState or IReadOnlySessionState in order to have the session state in the context parameter (the one that is passed to the ProcessRequest() routine.
Luckilly both interfaces are just marker interfaces, meaning you are not required to implement any additional methods.
So something like this:
public class ChbHandler : IHttpHandler, IRequiresSessionState
{
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
HttpRequest req = context.Request;
HttpResponse resp = context.Response;
resp.Write("HttpHandler: Hello World!");
}
#endregion
}
Should work just fine for you.
The only change is : IRequiresSessionState in the list of Interfaces that the class will inherit.
No comments:
Post a Comment