How to manage the controller’s session state?
Simply we can decorate the controller class with “SessionState” attribute. [SessionState()] attribute accepts SessionStateBehaviour enumeration.
SessionStateBehaviour enumeration has the following constants.
- SessionStateBehavior.Default – ASP.NET default logic is used to determine the session state behavior for the request.
- SessionStateBehavior.Required – Full read-write session state behavior is enabled for the request.
- SessionStateBehavior.ReadOnly – Read only session state is enabled for the request.
- SessionStateBehavior.Disabled – Session state is not enabled for processing the request.
We decorated controller class with [SessionState(SessionStateBehaviour.Disabled)] to disable the session. Important point to remember when we are disabling session of the controller, we should n’t use TempData Dictionary to store any values with action method controller, it uses session to store it’s values. If you use TempData Dictionary when session is disabled on controller it will throw an exception “The SessionStateTempDataProvider class requires session state to be enabled“.
Note: In earlier release of MVC3 (Beta) [SessionState()] attribute was refereed as [ControllerSessionState()]