相关疑难解决方法(0)

ASP.Net MVC控制器构造函数中的会话为null

为什么Session在控制器的构造函数中为null?它可以从Action方法访问.据推测,因为MVC路由框架负责新建一个Controller,所以它当时没有(重新)实例化Session.

有谁知道这是否是设计的,如果是的话,为什么?

[我已设法通过使用延迟加载模式来解决问题.]

asp.net-mvc session

84
推荐指数
4
解决办法
6万
查看次数

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

如果没有路由,HttpContext.Current.Session那么我知道它StateServer正在运行.当我路由我的请求时,HttpContext.Current.Sessionnull在路由页面中.我在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" /> …
Run Code Online (Sandbox Code Playgroud)

c# asp.net routing session-variables

46
推荐指数
4
解决办法
11万
查看次数

HttpContext.Current.Session null项

我存储在HttpContext.Current.Session当前用户中,SiteUser是单音类,它呈现当前活动的siteuser,当他记录我在控制器中创建新的SiteUser()并在构造函数中将他添加到会话中:

public static SiteUser Create()
{
    HttpContext.Current.Session[sessionKey] = new SiteUser();

    return HttpContext.Current.Session[sessionKey] as SiteUser;
}
Run Code Online (Sandbox Code Playgroud)

然后,对于服务器服务的每个请求,我检查用户是否在会话中可用:

public static SiteUser Current
{
    get
    {
        if (HttpContext.Current.Session == null || HttpContext.Current.Session[sessionKey] == null)
        {
            throw new SiteUserAutorizationExeption();
        }

        return HttpContext.Current.Session[sessionKey] as SiteUser;
    }
}
Run Code Online (Sandbox Code Playgroud)

否则我将生成非auth用户异常并将其重定向到登录页面.但有时HttpContext.Current.Session [sessionKey]为null,但HttpContext.Current.Session不为null且FormsAuthenticationTicket可用且Expired属性也为false.有人可以帮助我,为什么HttpContext.Current.Session [sessionKey]可以为空?

UPD:webconfig会话设置:<sessionState mode="InProc" timeout="20" />

UPD:sessionKey是:public const string sessionKey = "SiteUser";

UPD2:我忘了添加,在会话中还存储了文化设置:

HttpContext.Current.Session["Culture"]
Run Code Online (Sandbox Code Playgroud)

当异常命中时,HttpContext.Current.Session [sessionKey]为null,culture-item不是

UPD3:我已下载源.NET Framework的符号表,并在更改集合项时在SessionStateItemCollection上设置断点.我解决了一些错误:1)所有的收集项都是空的 - "文化"正在设置2)它发生在会话结束事件我无法理解它是怎么回事,因为在web.config会话超时设置为20

.net asp.net asp.net-mvc-3

3
推荐指数
1
解决办法
2万
查看次数