Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext.Current.Session is null when routing requests

Without routing, HttpContext.Current.Session is there so I know that the StateServer is working. When I route my requests, HttpContext.Current.Session is null in the routed page. I am using .NET 3.5 sp1 on IIS 7.0, without the MVC previews. It appears that AcquireRequestState is never fired when using the routes and so the session variable isn't instantiated/filled.

When I try to access the Session variables, I get this error:

base {System.Runtime.InteropServices.ExternalException} = {"Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>.

While debugging, I also get the error that the HttpContext.Current.Session is not accessible in that context.

--

My web.config looks like this:

<configuration>   ...   <system.web>     <pages enableSessionState="true">       <controls>         ...       </controls>     </pages>     ...   </system.web>   <sessionState cookieless="AutoDetect" mode="StateServer" timeout="22" />   ... </configuration> 

Here's the IRouteHandler implementation:

public class WebPageRouteHandler : IRouteHandler, IRequiresSessionState {     public string m_VirtualPath { get; private set; }     public bool m_CheckPhysicalUrlAccess { get; set; }      public WebPageRouteHandler(string virtualPath) : this(virtualPath, false)     {     }     public WebPageRouteHandler(string virtualPath, bool checkPhysicalUrlAccess)     {         m_VirtualPath = virtualPath;         m_CheckPhysicalUrlAccess = checkPhysicalUrlAccess;     }      public IHttpHandler GetHttpHandler(RequestContext requestContext)     {         if (m_CheckPhysicalUrlAccess             && !UrlAuthorizationModule.CheckUrlAccessForPrincipal(                    m_VirtualPath,                    requestContext.HttpContext.User,                    requestContext.HttpContext.Request.HttpMethod))         {             throw new SecurityException();         }          string var = String.Empty;         foreach (var value in requestContext.RouteData.Values)         {             requestContext.HttpContext.Items[value.Key] = value.Value;         }          Page page = BuildManager.CreateInstanceFromVirtualPath(                         m_VirtualPath,                          typeof(Page)) as Page;// IHttpHandler;          if (page != null)         {             return page;         }         return page;     } } 

I've also tried to put EnableSessionState="True" on the top of the aspx pages but still, nothing.

Any insights? Should I write another HttpRequestHandler that implements IRequiresSessionState?

Thanks.

like image 524
Loki Avatar asked Oct 20 '08 11:10

Loki


People also ask

Why HttpContext current session is null?

Session will never be null if you have any variable in the Session. HttpContext. Current. Session represents a collection of all the session values that you are storing in the session.

Is HttpContext ever null?

Clearly HttpContext. Current is not null only if you access it in a thread that handles incoming requests.

What is use of HttpContext current session?

The Session property provides programmatic access to the properties and methods of the HttpSessionState class. In order to use session state you have to enable it.

Why session is null in MVC?

Why is Session null in the constructors of Controllers? It can be accessed from Action methods. Presumably, because the MVC Routing framework is responsible for newing-up a Controller, it just hasn't (re-)instantiated the Session at that point.


1 Answers

Got it. Quite stupid, actually. It worked after I removed & added the SessionStateModule like so:

<configuration>   ...   <system.webServer>     ...     <modules>       <remove name="Session" />       <add name="Session" type="System.Web.SessionState.SessionStateModule"/>       ...     </modules>   </system.webServer> </configuration> 

Simply adding it won't work since "Session" should have already been defined in the machine.config.

Now, I wonder if that is the usual thing to do. It surely doesn't seem so since it seems so crude...

like image 151
Loki Avatar answered Sep 23 '22 10:09

Loki