MVC 3 Session.Abandon() 在设置 TempData[“myvalue”] = “foo” 之前调用导致下一个控制器将 TempData 设为 null

发布于 2024-12-06 06:26:05 字数 678 浏览 0 评论 0原文

我有一个控制器将逻辑寻找:会话值

 //checks value null etc..  for existing record in session memory.
 Session["certnum"]  

然后在控制器中我决定有一个条件:

 //is called to initiate a New Record that will be created.
 Session.Abandon();

但是在程序编码中是 Session.Abandon();在创建 TempData["myobject"] = "foo" 之前出现,并且在单步执行代码后,立即窗口中的 TempData 显示了我的值,一切看起来都很好。然后在重定向到另一个控制器时:

return RedirectToAction("ChildInfo", "NewRecord");  

此 ChildInfo 方法不再具有 TempData 值...现在它为 null。在设置 TempData 值之前调用了会话放弃方法,不确定这是否是 MVC 会话的错误,但这对我来说毫无意义。如果我正在创建一个新的轻量级会话 TempData,那么它应该保留到下一个控制器。如果我删除 Session.Abandon() 方法,则 TempData 值将继续像以前一样工作。

I have a controller will logic that looks for a: Session value

 //checks value null etc..  for existing record in session memory.
 Session["certnum"]  

Then in the controller I had decided to have a condition where:

 //is called to initiate a New Record that will be created.
 Session.Abandon();

However In the procedural coding is that Session.Abandon(); comes before the creation of TempData["myobject"] = "foo" , and upon stepping through the code the TempData in immediate window shows my value and all seems good. Then upon redirect to another controller:

return RedirectToAction("ChildInfo", "NewRecord");  

This ChildInfo method no longer has the TempData value ... Now it is null. The Session Abandon Method was called way before the TempData value was set, not sure if this is a bug with MVC Sessions, but that make zero sense to me. If I am creating a new lighweight session TempData, then it should persist to the next controller. If I remove the Session.Abandon() method then the TempData value persist working as it did previously.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

数理化全能战士 2024-12-13 06:26:05

Session.Abandon() 方法在请求结束时清除当前会话,这就是它的设计目的。

请参阅 http://msdn.microsoft.com/ en-us/library/system.web.sessionstate.httpsessionstate.abandon.aspx

如果您想重定向到不同的操作,您确实需要像您所做的那样调用重定向。如果您使用Abandon(),请求将获得一个新的会话 ID。

如果您想从会话中删除某些内容,则需要使用 Session.RemoveSession.RemoveAll 方法(此外 Clear 可用于与RemoveAll 执行相同的操作,这可以通过以下方式完成:

Session.Remove(itemToRemove);

Session.RemoveAll()

通过使用这两个选项中的任何一个,您可以从会话中删除部分或全部先前存储的数据,而实际上不会导致在会话上重新生成会话 ID。下一个请求。

The Session.Abandon() method clears the current session at the end of the request, that it what it is designed to do.

See http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.abandon.aspx

If you want to redirect to a different action, you do need to call the redirect like you have done. If you use Abandon() the request will get a new session id.

If you want to remove something from a session you need to use the Session.Remove or Session.RemoveAll methods (Also Clear can be used to do the same as RemoveAll. This would be done by:

Session.Remove(itemToRemove);

or

Session.RemoveAll()

By using either of these two options you can remove some or all previously stored data from the session without actually causing the session id to be regenerated on the next request.

我一直都在从未离去 2024-12-13 06:26:05

Session.Abandon 方法不会清除会话对象,它只是标记不应保留它。会话对象在当前请求期间仍然完好无损。

当响应完成后,会话对象就被废弃,这样下次浏览器发出请求时,服务器就必须建立一个新的会话对象。当会话对象被放弃时,您在整个请求期间放入会话对象中的任何内容都会消失。

当您进行重定向时,重定向页面将作为响应发送到浏览器,然后浏览器请求新页面。如果您将会话对象标记为放弃,然后进行重定向,则新页面将获得新的会话对象。

The Session.Abandon method doesn't clear the session object, it only flags that it should not be kept. The session object is still intact during the current request.

When the response is complete, the session object is abandoned, so that the next time the browser makes a request, the server has to set up a new session object. Anything that you put in the session object during that entire request goes away when the session object is abandoned.

When you make a redirect, a redirection page is sent as response to the browser, which then requests the new page. If you mark the session object to be abandoned and then do a redirect, the new page will get the new session object.

橪书 2024-12-13 06:26:05

这就是它应该如何工作的。

Session.Abandon 不会立即终止会话。它持续到页面末尾。然后,在加载下一个页面时,将创建一个新会话。

This is how it's supposed to work.

Session.Abandon does not kill the session immediately. It last until the end of the page. Then, upon the next page load, a new session is created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文