5

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.

4
  • The important thing here is that TempData relies on session. The session is gone hence TempData is gone. Sep 24, 2011 at 1:37
  • @Adam Yes I realize that however I am calling Session.Abandon() before creating a TempData value, I'm in the same actionresult method of a controller, but still, that seems just crazy to me. Sep 25, 2011 at 7:54
  • why would it be crazy? you are resetting an object required for tempdata. Thats like saying "I delete all the rows in the table, but I don't understand why on the next request the row I am going to request isn't there?" : ) Sep 25, 2011 at 15:04
  • If I thought that I was destroying the entire capability of using a session, that makes sense. However, for your analogy if I explicitly deleted all rows in a datatable, then obviously I can't READ those row(s) from from datatable, however if I figured that I was actually ADDING a row back into the datatable ( with the analogy ) that I was creating a NEW session object (Tempdata) and that It is simply encapsulated in the method and not even allowing this NEW session to persist. Hey I disagree with why .net this is how it works, it is all about picking my battles. I appreciate the feedback. Sep 26, 2011 at 5:11

3 Answers 3

11

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.

0
1

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.

0
0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.