Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp.net中检索会话值并签入其他页面_C#_Asp.net_Session - Fatal编程技术网

C# 如何在asp.net中检索会话值并签入其他页面

C# 如何在asp.net中检索会话值并签入其他页面,c#,asp.net,session,C#,Asp.net,Session,我想检查权限列表中是否有权限,但是if语句抛出了一个错误 var permissionList = Session["Permissions"]; string check = "Create Groups"; if (permissionList.Any(item => item.Equals(check))) { // results contains the value in check

我想检查权限列表中是否有权限,但是if语句抛出了一个错误

var permissionList = Session["Permissions"];         
        string check = "Create Groups";
        if (permissionList.Any(item => item.Equals(check)))
        {
            // results contains the value in check 
        }

只需调用
stringvalue=(String)会话[“Key”]以获取值

Session[“Key”]
将在Session中提取该键的值。如果密钥不存在,它将返回Null

你也可能想好好读一读

更新
为了响应您的编辑,请确保您的项目具有可用性。Any是Linq扩展。

更新:将底部代码更改为:

'object' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

看起来permissionList正在填充,即从会话中提取数据的代码工作正常。如果不是这种情况,那么您将收到NullReference异常

我猜真正的罪魁祸首是“var”关键字。从会话中提取PermissionList时,它将作为“对象”检索。尝试显式强制转换permissionList对象,即

using System.Collections.Generic;
using System.Linq;
List permissionList=会话[“权限”];

var permissionList=(List)会话[“权限”];

@John更新了答案。Any是Linq添加的扩展方法。确保将System.Core.dll作为项目的引用程序集和正在使用的System.Linq;顶部的语句。
asp
的标记说明中“请勿使用此标记”的哪一部分您不明白?为您修复了代码。谢谢你的回复,它引起了我的注意:)。
IList<String> collection = (IList<String>) Session["Permissions"];

string check = "Create Groups";

if (collection.Any(item => item.Equals(check)))
{
   // results contains the value in check 
}
using System.Collections.Generic;
using System.Linq;
List<string> permissionList = Session["Permissions"]; 
var permissionList = (List<string>)Session["Permissions"];