ASP.NET MVC - 会话为空 [英] ASP.NET MVC - Session is null

查看:23
本文介绍了ASP.NET MVC - 会话为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .net4 上有一个 MVC3 应用程序,它的会话在开发环境中工作,但不在生产环境中工作.
在生产中,我记录了 sessionID,它在我从会话中设置和获取的那一刻是相同的.

当我尝试获取会话时,我收到 Null Exception.

这是我访问会话的方式:

公共静态类HandlersHttpStorage{公共静态字符串 TestSession{得到{return HttpContext.Current.Session["time"];//这是空的}放{HttpContext.Current.Session.Add("time", value);//DateTime.Now.ToString()}}}

让我担心的是生产中的行为与开发中的行为不同,即使 web.config 是相同的.

解决方案

解决方案 1:

链接: HttpContext.Current.路由请求时会话为空

明白了.其实挺傻的在我删除 & 后它起作用了像这样添加了 SessionStateModule:

<预><代码><配置>...<system.webServer>...<模块><删除名称=会话"/><add name="Session" type="System.Web.SessionState.SessionStateModule"/>...</模块></system.webServer></配置>

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

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

解决方案 2:

链接: HttpContext.Current.Session null item

sessionKey 可能会改变,你可能只需要这样做:

HttpContext.Current.Session["CurrentUser"]

或者会话可能过期,检查超时:

http://msdn.microsoft.com/en-us/library/h6bb9cz9(VS.71).aspx

或者您可能正在其他地方设置会话值,通常我通过一个属性控制对会话/上下文对象的访问

static readonly string SESSION_CurrentUser = "CurrentUser";公共静态站点用户创建(){SiteUser.Current = new SiteUser();返回 SiteUser.Current;}公共静态站点用户当前{得到 {if (HttpContext.Current.Session == null || HttpContext.Current.Session[SESSION_CurrentUser] == null) {抛出新的 SiteUserAutorizationExeption();}返回 HttpContext.Current.Session[SESSION_CurrentUser] 作为 SiteUser;}放 {if (!HttpContext.Current.Session == null) {HttpContext.Current.Session[SESSION_CurrentUser] = 值;}}}

I have an MVC3 application on .net4 that its session working in the dev Environment, but not in the production.
In the production I logged the sessionID and the it is the same in the moment I Set and Get from the session.

When I try to get the session I am getting Null Exception.

This is how I access the session:

public static class HandlersHttpStorage
{
    public static string TestSession
    {
        get
        {
            return HttpContext.Current.Session["time"];//This is null
        }
        set
        {
            HttpContext.Current.Session.Add("time", value);//DateTime.Now.ToString()
        }
    }
}

What's makes me worried is that the behavior in the production is different than the development, even though the web.config is the same.

解决方案

Solution 1:

Link: HttpContext.Current.Session is null when routing requests

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>

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...

Solution 2:

Link: HttpContext.Current.Session null item

sessionKey may be changing, you probably only need to do:

HttpContext.Current.Session["CurrentUser"]

Or the session may be expiring, check the timeout:

http://msdn.microsoft.com/en-us/library/h6bb9cz9(VS.71).aspx

Or you may be setting the session value from somewhere else, normally i control access to Session/Context object through one property

static readonly string SESSION_CurrentUser = "CurrentUser";

public static SiteUser Create() {     
 SiteUser.Current = new SiteUser();      
 return SiteUser.Current;
}

public static SiteUser Current {     
 get {         
  if (HttpContext.Current.Session == null || HttpContext.Current.Session[SESSION_CurrentUser] == null) {             
   throw new SiteUserAutorizationExeption();         
  }          
  return HttpContext.Current.Session[SESSION_CurrentUser] as SiteUser;     
 } 
 set {
  if (!HttpContext.Current.Session == null) {
   HttpContext.Current.Session[SESSION_CurrentUser] = value;
  }
 }
} 

这篇关于ASP.NET MVC - 会话为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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