gpt4 book ai didi

c# - 如何不删除单个特定 session 变量?

转载 作者:太空宇宙 更新时间:2023-11-03 20:07:32 25 4
gpt4 key购买 nike

我在删除 session 变量时遇到问题..

假设我有 30 个 session 变量...我想删除 29 个 session 变量,但只有一个。我该怎么做,请帮助我!

我尝试使用 Session.RemoveAll() 删除所有 session 。但我的要求是删除除一个 session 之外的所有 session 。我该怎么做?

最佳答案

您可以使用 Session.Remove 从 session 集合中使用 key 删除。

您可以遍历您的 session 集合

for (int i = 0; i < Session.Contents.Count; i++)
{
var key = Session.Keys[i];
var value = Session[i];

//remove the key except one
if(key!="youkey")
Session.Remove(key);

}

编辑1

根据@JoachimIsaksson 的建议

 for (int i = 0; i < Session.Contents.Count; i++)
{
var key = Session.Keys[i];
var value = Session[i];

//remove the key except one
if(key!="youkey")
{
Session.Remove(key);
i++;
}

}

编辑2

正如@JoachimIsaksson 所建议的,我不确定是否正确

 for (int i = 0; i < Session.Contents.Count; i++)
{
var key = Session.Keys[i];
var value = Session[i];

//remove the key except one
if(key!="youkey")
{
Session.Remove(key);
i--;
}

}

关于c# - 如何不删除单个特定 session 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22113480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com