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

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

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 实现 IRequiresSessionState?

谢谢。

有帮助吗?

解决方案

知道了。实际上相当愚蠢。我删除<!> amp后它工作了像这样添加了SessionStateModule:

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

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

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

其他提示

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

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

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

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

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

不错的工作!我一直遇到完全相同的问题。添加和删​​除会话模块对我来说也非常有效。然而它并没有被 HttpContext.Current.User 返回,所以我用 FormsAuth 模块尝试了你的小技巧,果然,做到了。

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

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

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

有关SessionState指令的更多信息,请参阅此处

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

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

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

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

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

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

此部分代码也没用:

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

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

更好的解决方案是

runAllManagedModulesForAllRequests是一个聪明的事情,尊重删除和重新插入会话模块。

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top