.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification