开发者

Access Session Variable created in page from MasterPage

I am creating some sessions on successful login and I need to开发者_如何学Go access them from my master page. How do I go about this?

public void showUser()
{
    if (!string.IsNullOrEmpty(Session["User"].ToString()))
    {
        Response.Write(Session["User"].ToString());
    }
    else
    {
        Response.Write("Not Logged In");
    }
}


The line (!string.IsNullOrEmpty(Session["User"].ToString())) is incorrect way and will certainly raise an exception is you don;t have session variable set. Because in such case, Session["User"] will return null value, so you should be checking against that.

i.e.

if (null != Session["User"]) 
{

Or

user = Session["User"];
if (null != user && user.ToString().Length > 0)
{
  // user logged in
}
else
{
  // not logged in
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜