gpt4 book ai didi

c# - session 正在跨 Controller 在 RedirectToAction 上重置

转载 作者:行者123 更新时间:2023-11-30 23:25:25 29 4
gpt4 key购买 nike

我有一个 loginController With Index Mathod as

public ActionResult Index(string something)
{
Session.Abandon();
Session["ActiveUser"] = _user;
return RedirectToAction("All", "User");
}

和 UserContoller 作为

     public ActionResult All()
{
var _currentUser = ((AuthUser)Session["ActiveUser"]);
}

问题是当重定向发生时 session 状态重置,我无法理解为什么?任何帮助或资源将不胜感激。

最佳答案

编辑

I had also written Session.Abandon(); in the start when I removed that from I code it started working fine.

是的,这可以解释。调用 Session.Abandon 将 Session 排队等待销毁/删除,并在您的初始请求(在重定向开始之前)完成处理时执行它。因此,您在该初始请求中对 Session(包括 TempData)所做的任何操作基本上都会被忽略,因为包括更改在内的整个 session 都将被丢弃。 From the documentation .

When the Abandon method is called, the current Session object is queued for deletion but is not actually deleted until all of the script commands on the current page have been processed. This means that you can access variables stored in the Session object on the same page as the call to the Abandon method but not in any subsequent Web pages.


there is no special configuration of Session in the webconfig @Igor

然后 session 状态未启用,这就是为什么您无法从 SessionTempData 检索任何内容的原因。您需要在 web.config 中配置 session 状态。如果您仅将此用于 TempData,则可以为 InProc 配置它。如果您想配置长期存储(跨多个请求的数据可用)并且您有多个服务器(即网络场)可以处理请求,您应该使用像 Sql Server 这样的存储来查看配置。

您可以直接在 web.config ( see SessionState documentation )

中更改配置
<sessionState mode="Off|InProc|StateServer|SQLServer"
cookieless="true|false"
timeout="number of minutes"
stateConnectionString="tcpip=server:port"
sqlConnectionString="sql connection string"
stateNetworkTimeout="number of seconds"/>

或使用 IIS(见下面的屏幕截图)。

IIS Session State

关于c# - session 正在跨 Controller 在 RedirectToAction 上重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37292357/

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