有没有办法迭代所有ASP.NET MVC 2会话密钥?

Kev*_*vin 2 asp.net-mvc-2

我想获得所有会话变量名称的列表.

就像是:

foreach(Key name in HttpContext.Session.Keys)
{
...
}
Run Code Online (Sandbox Code Playgroud)

fea*_*net 9

你的代码有效!只是string不要使用Key

foreach (string key in Session.Keys)
{
    var value = Session[key];
}
Run Code Online (Sandbox Code Playgroud)

或者另类

for (int i = 0; i < Session.Contents.Count; i++)
{
    var key = Session.Keys[i];
    var value = Session[i];
}
Run Code Online (Sandbox Code Playgroud)