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

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

问题描述

如果没有路由, HttpContext.Current.Session 是有,所以我知道,的StateServer 正在工作。当我的路线我的请求, HttpContext.Current.Session 在路由页面。我使用.NET 3.5 SP1在IIS 7.0上,没有MVC previews。看来使用路线和时的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.

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

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

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

调试时,我也得到了 HttpContext.Current.Session 是不是在这种情况下。

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

-

我的的web.config 是这样的:

<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真在aspx页面的顶部,但仍,什么都没有。

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

任何见解?我应该写另一个的Htt prequestHandler 实现 IRequiresSessionState

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

现在,我不知道这是做平常的事情。它肯定似乎不那么因为看起来那么粗...

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路由请求时为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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