【问题标题】:How to check if session value is null or session key does not exist in asp.net mvc - 5如何检查会话值是否为空或 asp.net mvc 中不存在会话密钥 - 5
【发布时间】:2017-01-25 15:32:30
【问题描述】:

我有一个 ASP.Net MVC - 5 应用程序,我想在访问之前检查会话值是否为空。但是我做不到。

//Set
System.Web.HttpContext.Current.Session["TenantSessionId"] = user.SessionID;
// Access
int TenantSessionId = (int)System.Web.HttpContext.Current.Session["TenantSessionId"];

我尝试了 SO 的许多解决方案

尝试

if (!string.IsNullOrEmpty(Session["TenantSessionId"] as string))
 {
                //The code
 }

请指导我。

错误:空引用

【问题讨论】:

  • 试试if (Session["TenantSessionId"] == null)!=
  • @Roma:谢谢。你摇滚!
  • 在尝试编程之前,您可能想学习如何编程?那里有一些好书可供您阅读,对您有很大帮助。
  • @Will:我想一起编程和学习。我想做更多的实践。仍然感谢您的投入。
  • @Will:我是初学者,所以会犯一些愚蠢的错误。

标签: c# asp.net asp.net-mvc asp.net-mvc-5


【解决方案1】:
if(Session["TenantSessionId"] != null)
{
  // cast it and use it
  // The code
}

【讨论】:

  • 我是否也需要检查裸session null。请指导我。
  • 我觉得没什么用
【解决方案2】:

[] 充当索引器(类似于类上的方法),在这种情况下,sessionnull,您无法对其执行索引。

试试这个..

if(Session != null && Session["TenantSessionId"] != null)
{
   // code
}

【讨论】:

  • 检查这个Session["TenantSessionId"] != null 对我有用。我需要检查裸session too吗?
  • 是的,正如我提到的[],这将充当indexer,因此,有时当session 变量中的值为空时会发生这种情况,那么它可能会抛出null reference 的错误.
  • 我得到“当前上下文中不存在名称'Session'”,我需要在此处添加任何命名空间吗?我做了一个 ctrl + dot(.),但没有修复它。
【解决方案3】:

NullReferenceException 来自于尝试强制转换空值。通常,您最好使用as 而不是直接强制转换:

var tenantSessionId = Session["TenantSessionId"] as int?;

这永远不会引发异常。如果未设置会话变量,tenantSessionId 的值将简单地为 null。如果你有一个默认值,你可以使用 null coalesce 运算符来确保总是有 some 值:

var tenantSessionId = Session["TenantSessionId"] as int? ?? defaultValue;

然后,它将是会话中的值或默认值,即永远不会为空。

您也可以直接检查会话变量是否为空:

if (Session["TenantSessionId"] != null)
{
    // do something with session variable
}

但是,您需要将会话变量的所有工作限制在此条件范围内。

【讨论】:

  • 我正在使用as,但我遇到了一些错误,SO“Steve Muecke”中的一位著名人物告诉我,最好进行经典类型转换。因为整数不是引用类型和所有这些。 P.S:我没有使用“int?”我最后只有int
  • 是的,您不能将as 与不可为空的值一起使用,因为如果无法转换该值,它会返回null 而不是引发异常。但是,如果你为 null 合并,你仍然可以存储在一个不可为空的对象中。
【解决方案4】:

在低于 5 的 C# MVC 版本中检查会话是否为空/Null。

if (!string.IsNullOrEmpty(Session["TenantSessionId"] as string))
{
    //cast it and use it
    //business logic
}

在 C# MVC Version 5 以上检查会话是否为空/Null。

if(Session["TenantSessionId"] != null)
{
    //cast it and use it
    //business logic
}

【讨论】:

    【解决方案5】:

    有时您只想检查密钥本身是否存在而不是内容。当您的会话键值也为空时,上述方法失败。

    例如:

    Session["myKey"] = null;
    if(Session["myKey"]!=null){}
    

    在上面的代码中,我想检查是否只有键(而不是值)存在,我会得到假。但关键确实存在。

    所以我可以分离这个存在检查的唯一方法是检查每个键。

    static bool check_for_session_key(string SessionKey)
    {
         foreach (var key in HttpContext.Current.Session.Keys)
         {
             if (key.ToString() == SessionKey) return true;
         }
         return false;
    }
    if(check_for_session_key("myKey")){} //checks for the key only
    

    如果你知道更好的方法,请告诉我。

    【讨论】:

      【解决方案6】:

      检查会话是否为空/Null

      if (!string.IsNullOrEmpty(Session["TenantSessionId"] as string)) {

      //这里写逻辑代码

      }

      【讨论】:

      • 在您的答案中添加一些信息,而不仅仅是代码。
      猜你喜欢
      • 2015-09-15
      • 1970-01-01
      • 2011-04-23
      • 2011-11-02
      • 1970-01-01
      • 2011-08-31
      • 2011-02-18
      • 2012-05-24
      相关资源
      最近更新 更多