June 17, 2014

Session management in ASP.Net MVC and C#

In ASP.NET MVC, we can manage session by four ways: Default, Disabled, ReadOnly and Required. By default, or if we do not provide a session mode, it is set to its Default mode. Session mode is defined in “SessionStateBehavior” class.
To set session mode of a controller, we can use “SessionState” attribute at the beginning of defined controller class. For example:


[SessionState(System.Web.SessionState.SessionStateBehavior.Default)]
public class HomeController : Controller
{

}


As we are defining the session mode of a particular controller, so its scope is limited to the respective controller only. Off course, for different controller we can set its different session mode.

Default mode has with its default and limited functionalities. Disabled mode restricts to store value in session. ReadOnly mode allows to read the session values but does not allow to write a value into session. Required mode allows to both read and write.

(This tutorial written under VS 2010, ASP.NET MVC 4)

No comments:

Post a Comment