大家好我有一个实用程序类中的函数,它返回当前会话的用户ID。 它抛出未设置为对象实例的对象引用?我如何检查它为空&删除此错误?在返回之前检查会话吗?
public static string GetSessionUserID
{
get
{
string userID = "";
if (System.Web.HttpContext.Current.Session["userID"].ToString() != null)
{
userID = System.Web.HttpContext.Current.Session["userID"].ToString();
}
if (userID == null)
{
throw new ApplicationException("UserID is null");
}
else
return userID;
}
}
感谢马修。解释真的很有帮助。你的意思是代替var吗? – 2010-09-08 05:02:20
@Popo,它们应该是等价的(编译器会用'object'替换'var')。不过,我同意在这里“对象”更清晰。 – 2010-09-08 05:22:44
谢谢马修。我prev。思想变量只存在于JavaScript中。 – 2010-09-08 07:17:45