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
How to know if a session has been set
提问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.