当用户单击注销按钮时,如何销毁 session (Session["Name"])?
我正在查看 MSDN 上的 ASP.NET API 引用,它似乎没有太多信息。似乎比较有限。但是我找不到 ASP.NET 类等的任何其他页面。
我试过:
Session.Abandon();
和
Session.Contents.Remove("Name");
它们都不起作用。 (我在 Google 搜索的论坛中找到了这些)
最佳答案
Abandon
方法应该有效(MSDN):
Session.Abandon();
如果您想从 session 中删除特定项目,请使用 (MSDN):
Session.Remove("YourItem");
编辑:如果您只想清除一个值,您可以这样做:
Session["YourItem"] = null;
如果你想清除所有键,请执行以下操作:
Session.Clear();
如果这些都不适合您,那么就会出现问题。我会检查您在何处分配值,并验证在您清除该值后它没有被重新分配。
简单检查:
Session["YourKey"] = "Test"; // creates the key
Session.Remove("YourKey"); // removes the key
bool gone = (Session["YourKey"] == null); // tests that the remove worked
关于c# - 如何终止 session 或 session ID (ASP.NET/C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5330268/