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

时间:2008-10-20 11:03:28

标签: c# asp.net routing session-variables

没有路由,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页面的顶部,但仍然没有。

任何见解?我应该写另一个实现HttpRequestHandler的{​​{1}}吗?

感谢。

11 个答案:

答案 0 :(得分:52)

知道了。实际上相当愚蠢。在我删除&amp;像这样添加了SessionStateModule:

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

简单地添加它将无效,因为“会话”应该已在machine.config中定义。

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

答案 1 :(得分:24)

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

默认情况下,此属性在MVC和动态数据项目中启用。

答案 2 :(得分:14)

runAllManagedModulesForAllRequests=true实际上是一个非常糟糕的解决方案。这使我的应用程序的加载时间增加了200%。更好的解决方案是手动删除和添加会话对象,并避免一起运行所有托管模块属性。

答案 3 :(得分:4)

这些解决方案都不适合我。我将以下方法添加到global.asax.cs中,然后Session不为null:

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

答案 4 :(得分:3)

干得好!我一直有同样的问题。添加和删​​除Session模块也非常适合我。然而它并没有被HttpContext.Current.User带回来所以我尝试了使用FormsAuth模块的小技巧,当然,这样做了。

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

答案 5 :(得分:2)

@Bogdan Maxim说的话。如果您没有使用外部sesssion状态服务器,请更改为使用InProc。

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

查看here以获取有关SessionState指令的更多信息。

答案 6 :(得分:0)

您似乎忘记在config文件中添加状态服务器地址。

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

答案 7 :(得分:0)

如果正常访问页面,配置部分看起来很有效。我已经尝试了其他配置,但问题仍然存在。

我怀疑问题是在Session提供程序中,因为它没有路由工作。

答案 8 :(得分:0)

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

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

这部分代码也没用:

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

它将始终返回页面,它是否为null。

答案 9 :(得分:0)

更好的解决方案是

runAllManagedModulesForAllRequest是一个聪明的事情,尊重删除和resinserting会话模块。

答案 10 :(得分:0)

我在会话适配器中缺少对System.web.mvc dll的引用,并添加相同修复了该问题。

希望它可以帮助其他人经历同样的情况。