问题
What I currently have is this:
if ((string)Session["PlanID"] == null)
{
string PlanID = Request.QueryString["PlanID"];
Session["PlanID"] = PlanID;
}
What I need is something like this:
if ((string)Session["PlanID"] == null) or if ((string)Session["PlanID"] == "")
{
string PlanID = Request.QueryString["PlanID"];
Session["PlanID"] = PlanID;
}
How would I do that?
回答1:
You can use IsNullOrEmpty from string.
if (string.IsNullOrEmpty((string)Session["PlanID"])) {
string PlanID = Request.QueryString["PlanID"];
Session["PlanID"] = PlanID;
}
回答2:
You can use the String.IsNullOrWhiteSpace Method.
This method also checks for null values
Indicates whether a specified string is null, empty, or consists only of white-space characters.
if (string.IsNullOrWhiteSpace((string)Session["PlanID"]))
{
string PlanID = Request.QueryString["PlanID"];
Session["PlanID"] = PlanID;
}
来源:https://stackoverflow.com/questions/41989625/testing-a-session-variable-for-null-or-empty-space