什么是确定一个会话变量是在C#null或空的最好方法? [英] What is the best way to determine a session variable is null or empty in C#?
问题描述
什么是为您在ASP.NET C#会话变量的存在的最好方法?
我喜欢用 String.IsNullOrEmpty
适用于字符串,并想知道是否有用于会话
类似的方法。目前,我所知道的唯一的方法是:
VAR sSession;
如果(会话[变]!= NULL)
{
sSession =会话[变]的ToString()。
}
其他
{
sSession =设置该;
会议[变量] = sSession;
}
要从其他人所说遵循。我往往有两层:
核心层。这是添加到几乎所有的Web应用程序项目 DLL中。在此我有一个SessionVars类确实为会话状态getter / setter方法繁重的工作。它包含code这样的:
公共类SessionVar
{
静态HttpSessionState会议
{
得到
{
如果(HttpContext.Current == NULL)
抛出新ApplicationException的(没有HTTP上下文,无会话获取!); 返回HttpContext.Current.Session;
}
} 公共静态吨得到< T>(字符串键)
{
如果(会话[关键] == NULL)
返回默认(T);
其他
回报(T)会话[关键]
} 公共静态无效的SET< T>(字符串键,T值)
{
会话[关键] =价值;
}
}
请注意仿制药用于获取任何类型的。
我你还需要增加getter / setter方法为特定类型,特别是字符串因为我经常preFER用的String.Empty的工作,而不是为null psented到用户变量$ P $。
例如:
公共静态字符串的GetString(字符串键)
{
字符串s =获取<串GT;(密钥);
返回小号== NULL?的String.Empty:S;
} 公共静态无效的SetString(字符串键,字符串值)
{
SET<串GT;(键,值);
}
等等......
然后我创建包装抽象的路程,使其达到应用模型。例如,如果我们有客户的详细信息:
公共类CustomerInfo
{
公共字符串名称
{
得到
{
返回SessionVar.GetString(CustomerInfo_Name);
}
组
{
SessionVar.SetString(CustomerInfo_Name,值);
}
}
}
您的想法吧? :)
注意:增加了接受的答案评论时只是有一个想法。务必确保使用状态服务器时,会话存放时对象序列化。它可以是太容易尝试和Web场在使用仿制药保存对象,它走的热潮。我对部署在工作中网农场,增加检查,在核心层我的code,看看对象是序列化,封装会议getter和setter的另一个好处:)
What is the best way to check for the existence of a session variable in ASP.NET C#?
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 sSession;
if (Session["variable"] != null)
{
sSession = Session["variable"].ToString();
}
else
{
sSession = "set this";
Session["variable"] = sSession;
}
To follow on from what others have said. I tend to have two layers:
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.
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.
e.g:
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);
}
And so on...
I then create wrappers to abstract that away and bring it up to the application model. For example, if we have customer details:
public class CustomerInfo
{
public string Name
{
get
{
return SessionVar.GetString("CustomerInfo_Name");
}
set
{
SessionVar.SetString("CustomerInfo_Name", value);
}
}
}
You get the idea right? :)
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屋!