C# 将会话变量值与字符串进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10686953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
comparing session variable value to a string
提问by drinu16
I am comparing the session variable to a string to check if the login type is administrator or not.
我将会话变量与字符串进行比较,以检查登录类型是否为管理员。
Code i am using :
我正在使用的代码:
if (Session["loggedInUsername"] == null)
{
btnLogin.Text = "Sign In";
lblWelcome.Text = "Welcome!";
hypManageRestaurants.Enabled = false;
hypManageReviews.Enabled = false;
hypPostReviews.Enabled = false;
}
else
{
if (Session["loggedInUserType"] == "Administrator")
{
hypManageRestaurants.Enabled = true;
hypManageReviews.Enabled = true;
hypPostReviews.Enabled = true;
}
else
{
hypManageRestaurants.Enabled = false;
hypManageReviews.Enabled = false;
hypPostReviews.Enabled = true;
}
lblWelcome.Text = "Welcome " + Session["loggedInUsername"];
btnLogin.Text = "Sign Out";
}
So first I am checking if any user has logged in or not. If the user logs in successfully, the session variable "loggedInUsername" will have the value of the username. If the "loggedInUsername" session variable is not empty, it will check "loggedInUserType" session variable for the type of the logged in user.
所以首先我要检查是否有任何用户登录。如果用户登录成功,会话变量“loggedInUsername”将具有用户名的值。如果“loggedInUsername”会话变量不为空,它将检查“loggedInUserType”会话变量以获取登录用户的类型。
Here comes the weird thing, the value of the "loggedInUserType" is exactly "Administrator" without the "", at the if function where I am comparing the session variable to the string "Administrator" is being skipped and goes to the else.
奇怪的事情来了,“loggedInUserType”的值正是没有“”的“Administrator”,在我将会话变量与字符串“Administrator”进行比较的if函数被跳过并转到else。
All session variables are getting values when the user logs in.
当用户登录时,所有会话变量都会获取值。
Below is the data which I am using to login and this record is the only record which have a type of "Administrator".
下面是我用来登录的数据,该记录是唯一具有“管理员”类型的记录。
Is there any other method to compare a session variable to a string
有没有其他方法可以将会话变量与字符串进行比较
采纳答案by Jaimal Chohan
Cast the object type value to a string
将对象类型值转换为字符串
((string)Session["loggedInUserType"]) == "Administrator"
回答by Marco
Try this:
尝试这个:
if (Session["loggedInUserType"].ToString().Trim()
.Equals("Administrator", StringComparison.InvariantCultureIgnoreCase))
回答by Carl Winder
Try
尝试
if(Convert.ToString(Session["loggedInUserType"]) == "Administrator) ...
if(Convert.ToString(Session["loggedInUserType"]) == "Administrator) ...
回答by Dave Lawrence
Are you sure that all whitespace has been trimmed from the end of the Session["loggedInUserType"]?
您确定从 Session["loggedInUserType"] 的末尾删除了所有空格吗?
回答by daryal
if (Session["loggedInUserType"].ToString() == "Administrator")
回答by Guffa
The Sessioncollection returns values of type Object, so when you compare that to a string you will be comparing the values of the object references, not comparing the string values.
该Session集合返回 type 的值Object,因此当您将其与字符串进行比较时,您将比较对象引用的值,而不是比较字符串值。
Cast the object reference to string:
将对象引用强制转换为string:
if (((string)Session["loggedInUserType"]) == "Administrator")
回答by SarahB
You can do this :
你可以这样做 :
string session = (string)Session["loggedInUserType"]
if (session == "Administrator")
or your Sessioncan be in a specific class with getters.
或者你Session可以在一个特定的类中使用吸气剂。
回答by Kashif Faraz
if(Convert.ToString(Session["loggedInUserType"]) == "Administrator)
In this way, no need to check null value becuase Convert.ToString handle Null value return "" empty string
这样就不需要检查空值了,因为 Convert.ToString 处理空值返回 "" 空字符串

