在 C# 中确定会话变量为 null 或为空的最佳方法是什么? [英] What is the best way to determine a session variable is null or empty in C#?

查看:26
本文介绍了在 C# 中确定会话变量为 null 或为空的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ASP.NET C# 中检查会话变量是否存在的最佳方法是什么?

What is the best way to check for the existence of a session variable in ASP.NET C#?

我喜欢将 String.IsNullOrEmpty() 用于字符串,并想知道 Session 是否有类似的方法.目前我所知道的唯一方法是:

I like to use String.IsNullOrEmpty() works for strings and wondered if there was a similar method for Session. Currently the only way I know of is:

 var session;
 if (Session["variable"] != null)
 {
     session = Session["variable"].ToString();
 }
 else
 {
     session = "set this";
     Session["variable"] = session;
 }

推荐答案

遵循其他人所说的.我倾向于有两层:

To follow on from what others have said. I tend to have two layers:

核心层.这是在添加到几乎所有网络应用项目的 DLL 中.在这个我有一个 SessionVars 类,它为会话状态 getter/setter 做繁重的工作.它包含如下代码:

The core layer. This is within a DLL that is added to nearly all web app projects. In this I have a SessionVars class which does the grunt work for Session state getters/setters. It contains code like the following:

public class SessionVar
{
    static HttpSessionState Session
    {
        get
        {
            if (HttpContext.Current == null)
                throw new ApplicationException("No Http Context, No Session to Get!");

            return HttpContext.Current.Session;
        }
    }

    public static T Get<T>(string key)
    {
        if (Session[key] == null)
            return default(T);
        else
            return (T)Session[key];
    }

    public static void Set<T>(string key, T value)
    {
        Session[key] = value;
    }
}

注意获取任何类型的泛型.

Note the generics for getting any type.

然后我还为特定类型添加 Getter/Setter,尤其是字符串,因为我经常更喜欢使用 string.Empty 而不是 null 来处理呈现给用户的变量.

I then also add Getters/Setters for specific types, especially string since I often prefer to work with string.Empty rather than null for variables presented to Users.

例如:

public static string GetString(string key)
{
    string s = Get<string>(key);
    return s == null ? string.Empty : s;
}

public static void SetString(string key, string value)
{
    Set<string>(key, value);
}

等等……

然后我创建包装器来抽象它并将其带到应用程序模型中.例如,如果我们有客户详细信息:

public class CustomerInfo
{
    public string Name
    {
        get
        {
            return SessionVar.GetString("CustomerInfo_Name");
        }
        set
        {
            SessionVar.SetString("CustomerInfo_Name", value);
        }
    }
}

你猜对了吗?:)

注意: 在对已接受的答案添加评论时,我有一个想法.使用状态服务器时,在将对象存储在 Session 中时,始终确保对象是可序列化的.在网络农场上尝试使用泛型来保存对象可能太容易了,并且它会蓬勃发展.我在工作时部署在 Web 场上,因此在核心层中添加了对我的代码的检查,以查看对象是否可序列化,这是封装会话 Getter 和 Setter 的另一个好处:)

NOTE: Just had a thought when adding a comment to the accepted answer. Always ensure objects are serializable when storing them in Session when using a state server. It can be all too easy to try and save an object using the generics when on web farm and it go boom. I deploy on a web farm at work so added checks to my code in the core layer to see if the object is serializable, another benefit of encapsulating the Session Getters and Setters :)

这篇关于在 C# 中确定会话变量为 null 或为空的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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