Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC optimization for Session.Clear(), Session.Abandon(), Session.RemoveAll()?

I am using a few sessions that should be terminated when the user is done. I stumbled on these 3 session killers. When is the best time to use these as I use sessions more time than not. Also, is there any other session termination I am not aware of?

like image 990
MrM Avatar asked Aug 23 '10 19:08

MrM


People also ask

What is the difference between session Clear () and session abandon () in asp net?

Clearing the session will not unset the session, it still exists with the same ID for the user but with the values simply cleared. Abandon will destroy the session completely, meaning that you need to begin a new session before you can store any more values in the session for that user.

What does session Clear () do?

The Session. clear() method is used to remove all cached objects associated with the session.

How can delete session data in MVC?

Session. Clear and Session. RemoveAll are identical; the latter just calls the former. They immediately remove all items stored in the session, but the session itself survives.

How can we maintain session in MVC?

ASP.NET MVC provides three ways (TempData, ViewData and ViewBag) to manage session, apart from that we can use session variable, hidden fields and HTML controls for the same.


1 Answers

Session.Clear and Session.RemoveAll are identical; the latter just calls the former. They immediately remove all items stored in the session, but the session itself survives. Session_OnEnd does not fire.

Session.Abandon doesn't actually clear the values immediately, it just marks the session to be abandoned at the end of the current request. You can continue to read the values for the rest of the request. If you write to the session later in the request, the new value will be quietly discarded at the end of the request with no warning. Session_OnEnd fires at the end of the request, not when Abandon is called.

like image 151
stevemegson Avatar answered Sep 19 '22 23:09

stevemegson