Testing a session variable for null or empty space

左心房为你撑大大i 提交于 2019-12-25 05:33:37

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!