Accessing Session State in HTTPHandlerFactory RRS feed

  • Question

  • User-1002471015 posted
    I have the following HTTPHandlerFactory 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 extension = Path.GetExtension(path); if (extension != ".css" && extension != ".js" && extension != ".gif" && extension != ".jpg") { int slashPos = url.LastIndexOf("/") + 1; string name = url.Substring(slashPos); // Find the websiteid TiffanyDreams.BLL.Website website = new TiffanyDreams.BLL.Website(); int websiteId = website.FindWebsiteID(name); // Redirect to customer website if websiteid is found if (websiteId != 0) { context.Session["WebsiteId"] = websiteId; path = HttpContext.Current.Server.MapPath("~/website.aspx"); return PageParser.GetCompiledPageInstance (url, path, 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) { } } } As you can see, i'm trying to set a Session variable (context.Session["WebsiteId"] = websiteId), the code compiles fine but i'm getting a runtime error on that line (Object reference not set to an instance of an object.) Does anyone know what I am doing wrong?
    Thursday, August 12, 2004 6:26 AM

All replies

  • User-1002471015 posted
    Does anyone know what I am doing wrong?
    Friday, August 13, 2004 5:06 AM
  • User-1002471015 posted
    I've still not figured this out, anyone got any suggestions?
    Monday, August 16, 2004 8:08 AM
  • User-666544324 posted
    The Session object is not yet created when your code is called. HttpHandlerFactories are invoked early on in the request lifecycle and Session is created after that. In order to accomplish this, you may want to consider using browser cookies to store that information
    Tuesday, August 17, 2004 8:57 PM