没有路由,HttpContext.Current.Session
是否存在所以我知道它StateServer
正在工作。当我路由我的请求时,HttpContext.Current.Session
在null
路由页面中。我在 IIS 7.0 上使用 .NET 3.5 sp1,没有 MVC 预览。使用路由时似乎AcquireRequestState
永远不会触发,因此会话变量不会被实例化/填充。
当我尝试访问 Session 变量时,我收到此错误:
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>.
在调试时,我还收到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;
}
}
我也尝试过放在EnableSessionState="True"
aspx 页面的顶部,但仍然没有。
Any insights? Should I write another HttpRequestHandler
that implements IRequiresSessionState
?
Thanks.