路由请求时 HttpContext.Current.Session 为 null [英] HttpContext.Current.Session is null when routing requests

查看:33
本文介绍了路由请求时 HttpContext.Current.Session 为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

当我尝试访问会话变量时,出现此错误:

base {System.Runtime.InteropServices.ExternalException} = {"Session state 只能在 enableSessionState 设置为 true 时使用,无论是在配置文件中还是在 Page 指令中.还请确保 System.Runtime.InteropServices.ExternalExceptionWeb.SessionStateModule 或自定义会话状态模块包含在 <configuration>.

在调试时,我也收到了在该上下文中无法访问 HttpContext.Current.Session 的错误.

--

我的 web.config 看起来像这样:

<预><代码><配置>...<system.web><pages enableSessionState="true"><控制>...</控制></页面>...</system.web><sessionState cookieless="AutoDetect" mode="StateServer" timeout="22"/>...</配置>

这是 IRouteHandler 的实现:

公共类 WebPageRouteHandler : IRouteHandler, IRequiresSessionState{公共字符串 m_VirtualPath { 获取;私人订制;}public bool m_CheckPhysicalUrlAccess { get;放;}public WebPageRouteHandler(string virtualPath) : this(virtualPath, false){}public WebPageRouteHandler(string virtualPath, bool checkPhysicalUrlAccess){m_VirtualPath = virtualPath;m_CheckPhysicalUrlAccess = checkPhysicalUrlAccess;}公共 IHttpHandler GetHttpHandler(RequestContext requestContext){如果(m_CheckPhysicalUrlAccess&&!UrlAuthorizationModule.CheckUrlAccessForPrincipal(m_虚拟路径,requestContext.HttpContext.User,requestContext.HttpContext.Request.HttpMethod)){抛出新的安全异常();}字符串 var = String.Empty;foreach(requestContext.RouteData.Values 中的 var 值){requestContext.HttpContext.Items[value.Key] = value.Value;}页面 page = BuildManager.CreateInstanceFromVirtualPath(m_虚拟路径,typeof(Page)) as Page;//IHttpHandler;如果(页面!= null){返回页;}返回页;}}

我也尝试将 EnableSessionState="True" 放在 aspx 页面的顶部,但仍然没有.

有什么见解吗?我应该编写另一个实现 IRequiresSessionStateHttpRequestHandler 吗?

谢谢.

解决方案

明白了.其实挺傻的在我删除 & 后它起作用了像这样添加了 SessionStateModule:

<预><代码><配置>...<system.webServer>...<模块><删除名称=会话"/><add name="Session" type="System.Web.SessionState.SessionStateModule"/>...</模块></system.webServer></配置>

简单地添加它是行不通的,因为会话"应该已经在 machine.config 中定义了.

现在,我想知道这是否是通常的做法.肯定不是这样,因为它看起来很粗糙......

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.

解决方案

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

这篇关于路由请求时 HttpContext.Current.Session 为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆