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.
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.RemoveorSession.RemoveAllmethods (AlsoClearcan be used to do the same asRemoveAll. This would be done by:or
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.