How To Create Session-less Controller in MVC3

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.

Sessionless Controller

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()]

Sharavan Kumar Kasagoni

Shravan is working as Senior Development Engineer in Pramati Technologies, earlier he worked with Thomson Reuters. He is responsible for design and development of RESTful APIs, Single Page Web Applications. He is awarded as Microsoft Microsoft Most Valuable Professional for years 2016, 2015, 2014 2013 and 2012 from Microsoft. He also is a Microsoft Certified Professional for Programming in HTML5 with JavaScript and CSS3 Specialist. Programming is his passion. He is core member of Microsoft User Group Hyderabad. In his overall experience of 7+ years, his expertise includes Azure, C#, Swift, ASP.NET MVC, HTML 5, TypeScript, Object Oriented JavaScript, JavaScript Frameworks (Angular, Angular 2, Backbone, React), iOS. He also experiments on next generation JavaScript and Web technologies. Shravan's Blog: Follow Shravan @ Twitter: @techieshravan