Access session from inside HttpHandler
Messages   Related Types
This message was discovered on ASPFriends.com 'aspnghttphandlers' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

David Brophy
Hi,

I'm using a HttpHandlerFactory to produce my pages, however, I'd like to =
be able
to use HttpContext.Current.Session in my GetHandler function, as below.

However, the HttpContext.Current.Session=3D=3Dnull. Do you know a work =
around, or is
it impossible?

public class MyHandlerFactory : IHttpHandlerFactory
{
    public virtual IHttpHandler GetHandler(HttpContext context, String
requestType, String url, String pathTranslated)
    {
        //Do lots of stuff here
        //Use HttpContext.Current.Session[] here
        return (IHttpHandler)aspxPage;
    }

    public virtual void ReleaseHandler(IHttpHandler handler){}
}

- David Brophy
Director, Cambro Limited,
(023) 80 679550=20
Click here to reveal e-mail address

Reply to this message...
 
    
Scott Guthrie (VIP)
You need to implement the System.Web.SessionState.IRequiresSessionState
interface on your IHttpHandler (note that this is a marker interface
with no methods to implement -- just add it to your class declaration).

The session state module checks IHttpHandlers for this interface at
runtime -- and will cause the session state intrinsic to be populated if
this interface is specified.

Hope this helps,

Scott

-----Original Message-----
From: David Brophy [mailto:Click here to reveal e-mail address]=20
Sent: Saturday, March 09, 2002 10:05 AM
To: aspnghttphandlers
Subject: [aspnghttphandlers] Access session from inside HttpHandler

Hi,

I'm using a HttpHandlerFactory to produce my pages, however, I'd like to
be able
to use HttpContext.Current.Session in my GetHandler function, as below.

However, the HttpContext.Current.Session=3D=3Dnull. Do you know a work
around, or is
it impossible?

public class MyHandlerFactory : IHttpHandlerFactory
{
    public virtual IHttpHandler GetHandler(HttpContext context,
String
requestType, String url, String pathTranslated)
    {
        //Do lots of stuff here
        //Use HttpContext.Current.Session[] here
        return (IHttpHandler)aspxPage;
    }

    public virtual void ReleaseHandler(IHttpHandler handler){}
}

- David Brophy
Director, Cambro Limited,
(023) 80 679550=20
Click here to reveal e-mail address

| [aspnghttphandlers] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspnghttphandlers.asp =3D JOIN/QUIT
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
    
David Rhodes
I have followed these instructions and implemented System.Web.SessionState.IRequiresSessionState in my HttpHandlerFactory and referenced System.Web.SessionState but am getting an error (Object reference not set to an instance of an object) when trying to set a Session variable. Here is my code

using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;

namespace TiffanyDreams.HttpHandler
{
    public class HandlerFactory : IHttpHandlerFactory, IRequiresSessionState
    {
        public virtual IHttpHandler GetHandler (HttpContext context, string requestType, string url, string path)
        {
            string fileName = Path.GetFileNameWithoutExtension(path);
            string extension = Path.GetExtension(path);

            if (fileName == "" && extension == "")
            {
                return PageParser.GetCompiledPageInstance
                    (url, context.Server.MapPath("~/default.aspx"), context);
            }
            else
            {
                if (extension != ".css" && extension != ".js" && extension != ".gif" && extension != ".jpg")
                {
                    // Find the websiteid
                    TiffanyDreams.BLL.Website website = new TiffanyDreams.BLL.Website();
                    int websiteId = website.FindWebsiteID( Path.GetFileNameWithoutExtension(path) );

                    // Redirect to customer website if websiteid is found
                    if (websiteId != 0)
                    {
                        // This does not work yet
                    //    context.Session["WebsiteId"] = websiteId;
                        return PageParser.GetCompiledPageInstance
                            (url, context.Server.MapPath("~/website.aspx"), context);
                    }
                    else
                    {
                        return PageParser.GetCompiledPageInstance (url, path, context);
                    }
                }
                else
                {
                    // Process the image/js/css
                    context.Response.WriteFile(url);
                    return null;
                }
            }
        }

        public virtual void ReleaseHandler (IHttpHandler handler)
        {
        }
    }
}

--------------------------------
From: David Rhodes
Reply to this message...
 
 
System.IO.Path
System.Web.HttpContext
System.Web.IHttpHandler
System.Web.IHttpHandlerFactory
System.Web.SessionState.IRequiresSessionState
System.Web.UI.PageParser




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com