路由请求时,HttpContext.Current.Session为空

没有路由, HttpContext.Current.Session在那里,所以我知道StateServer正在工作。 当我路由我的请求时, HttpContext.Current.Session在路由页面中为null 。 我在IIS 7.0上使用.NET 3.5 sp1,没有MVC预览。 看来AcquireRequestState在使用路由时永远不会被触发,因此sessionvariables没有被实例化/填充。

当我尝试访问会话variables,我得到这个错误:

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

在debugging的时候,我也得到了这个错误: HttpContext.Current.Session在这个上下文中是不可访问的。

我的web.config如下所示:

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

这是IRouteHandler实现:

 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; } } 

我也试着把aspx页面顶部的EnableSessionState="True" ,但还是没有。

任何见解? 我应该写另一个实现IRequiresSessionState HttpRequestHandler吗?

谢谢。

得到它了。 其实很愚蠢 它工作后,我删除和添加SessionStateModule像这样:

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

简单地添加它将不起作用,因为“Session”应该已经在machine.config定义了。

现在,我想知道这是否是通常的事情。 这看起来似乎不那么显得那么粗糙

只需在web.config中将属性runAllManagedModulesForAllRequests="true"添加到system.webServer\modules

该属性在MVC和dynamic数据项目中默认启用。

runAllManagedModulesForAllRequests=true实际上是一个真正的不好的解决scheme。 这使我的应用程序的加载时间增加了200%。 更好的解决scheme是手动删除和添加会话对象,并避免将所有托pipe模块属性全部运行在一起。

不错的工作! 我一直有完全相同的问题。 添加和删​​除会话模块也适用于我。 然而,它并没有被HttpContext.Current.User带回来,所以我尝试了一下FormsAuth模块的小技巧,果然,它做到了。

 <remove name="FormsAuthentication" /> <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"/> 

博格丹·马克森说什么? 或者如果您不使用外部sesssion状态服务器,则更改为使用InProc。

 <sessionState mode="InProc" timeout="20" cookieless="AutoDetect" /> 

在这里查看有关SessionState指令的更多信息。

我有一个类似的问题,并“解决”它: ASP.NET路由 – 做自定义路由完全跳过Global.asax中的一切?

更好的解决scheme是

runAllManagedModulesForAllRequest是一个聪明的事情去做尊重删除和树脂会话模块。

ALK。

看来你已经忘记在configuration文件中添加你的状态服务器地址了。

  <sessionstate mode="StateServer" timeout="20" server="127.0.0.1" port="42424" /> 

如果正常访问页面,config部分听起来很好。 我已经尝试了其他configurationbuild议,但问题仍然存在。

我怀疑问题是在会议供应商,因为它没有路由工作。

我认为这部分代码会对上下文进行更改。

  Page page = BuildManager.CreateInstanceFromVirtualPath( m_VirtualPath, typeof(Page)) as Page;// IHttpHandler; 

这部分代码也是无用的:

  if (page != null) { return page; } return page; 

它会一直返回页面,不pipe它是否为空。

我在会议适配器中缺less对System.web.mvc dll的引用,并添加了相同的问题。

希望这将有助于其他人经历相同的情况。

上述解决scheme都不适合我。 我将下面的方法添加到global.asax.cs然后Session不是null:

 protected void Application_PostAuthorizeRequest() { HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required); }