C# 如何知道会话是否已设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15076567/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:58:58  来源:igfitidea点击:

How to know if a session has been set

c#asp.net

提问by Misters

In php i used to use

在 php 中我曾经使用

session_start();
if(isset(SESSION["user"]))
{
   //session is set
}
els{
    // there is no session 
}

but do i do that in asp.net? I mean. What code can tells wheather a session is set or not

但我在asp.net 中这样做吗?我的意思是。什么代码可以告诉是否设置了会话

ex: asp.net c#

例如:asp.net c#

//login.aspx
SESSION["USER"];

//user_profile.aspx
if(SESSION["USER"])// how do i validate that??
{

}

采纳答案by jTC

SESSION["USER"]; //this should throw an error since it's not setting a value and not a method.

You can test your session values like this:

您可以像这样测试会话值:

if (Session["USER"] != null)
{
    //do something interesting
}

回答by Abe Miessler

If you want to check for the existance of a session variable this will be fine:

如果您想检查会话变量是否存在,这很好:

if(Session["USER"] != null)
{
    //If you get here a session variable "USER" exists...
}

Though it is possible to disable session statein an asp.net application it is very rare to see that.

尽管可以在 asp.net 应用程序中禁用会话状态,但这种情况很少见。

回答by Soner G?nül

From php side, cince issetfunction

从 php 端,cince isset函数

Determine if a variable is set and is not NULL.

确定变量是否已设置且不为 NULL。

Just check if this session nullor not like:

只需检查此会话是否null喜欢:

if(Session["USER"] != null)
{
  // Do something
}