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

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

问题描述

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

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.

当我尝试访问 Session 变量时,出现以下错误:

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

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

在调试时,我还收到 HttpContext.Current.Session 在该上下文中不可访问的错误.

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

--

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

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>

这是 IRouteHandler 的实现:

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

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

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

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

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

谢谢.

推荐答案

知道了.相当愚蠢,实际上.在我删除 & 后它起作用了像这样添加 SessionStateModule:

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>

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

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 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:来自 ASP.NET MVC CMS 数据库的动态路由 下一篇:解密 .NET clr20r3 异常参数 P1..P10

相关文章

最新文章