0

I am working on an HttpModule class for collecting performance and run time data of ASP.Net applications.

I've figured out how to access the Session for information on ASPX files and classes implementing IHttpHandler (such as custom web resources or axd files), but I can't find a way to access the instance of HttpContext that is passed into ProcessRequest() on a generic handler (ASHX file).

Application.Session throws an HttpException

(Session state is not available in this context)

and the HttpContext.Current.Session == null.

Thanks!

amber
  • 1,067
  • 3
  • 22
  • 42

1 Answers1

2

If you add a new Generic Handler to your project, it shoudl look like this

public class Handler1 : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

Where context is passed as the HttpContext you can use.

And if you need to read a Session you need to add IReadOnlySessionState or IRequiresSessionState (for difference see here)

using System.Web.SessionState;

public class Handler1 : IHttpHandler, IReadOnlySessionState
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • This doesn't answer my question directly, but it's useful in that it challenges an assumption I had about passed in HttpContext passed in. Since my HttpModule has to to work with ASP.Net applications that we didn't write, and thus have no control over whether or not generic handlers have access to Session state and I need to account for that. Thanks! – amber May 15 '19 at 16:40