Your browser does not support JavaScript! Please enable script of your browser.
 
"Coding , A Rhythmic Literary Job"
Handy Tricks For Beginners & Professionals
ASP.NET MVC 5, 4 - Tutorial, Article Pdf, Training, Code Snippets, Reference Manual, Model, View, Controller, Routing System, Unobtrusive Validation, Html Helpers, Razor Engine, Data Annotations, Dependancy Injection, Database Access, Security, Configuration and Deployment

Controlling Session Behavior in Asp.Net MVC4

Posted By : Shailendra Chauhan, 06 Jan 2013
Updated On : 30 Nov 2013
  Version Support : MVC3 & MVC4
Keywords : Session state in mvc,disable session in mvc,session less controller,session less action method

By default, Asp.Net MVC support session state. Session is used to store data values across requests. Whether you store some data values with in the session or not Asp.Net MVC must manage the session state for all the controllers in your application that is time consuming. Since, session is stored on server side and consumes server memory, hence it also affect your application performance.

Session Less Controller

If some of the controllers of your Asp.Net MVC application are not using session state features, you can disable session for those controller and can gain slight performance improvement of your application. You can simplify session state for your application by using available options for session state.

In Asp.Net MVC4, SessionState attribute provides you more control over the behavior of session-state by specifying the value of SessionStateBehavior enumeration as shown below:

SessionStateBehavior Enumeration's Value
Value
Description
Default
The default Asp.Net behavior is used to determine the session state behavior.
Disabled
Session state is disabled entirely.
ReadOnly
Read-only session state behavior is enabled.
Required
Full read-write session state behavior is enabled.

In Asp.Net MVC, TempData use session state for storing the data values across requests. Hence, when you will disabled the session state for the controller, it will throw the exception as shown below:

What do you think?

I hope you will enjoy the tips while programming with Asp.Net MVC. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

About the Author
Shailendra Chauhan works as Software Analyst at reputed MNC and has more than 5 years of hand over Microsoft .NET technologies. He is a .NET Consultant and is the founder & chief editor of www.dotnet-tricks.com and www.dotnetinterviewtricks.com blogs. He is an author of book ASP.NET MVC Interview Questions and Answers.
He loves to work with web applications and mobile apps using Microsoft technology including ASP.NET, MVC, C#, SQL Server, WCF, Web API, Entity Framework,Cloud Computing, Windows Azure, jQuery, jQuery Mobile, Knockout.js, Angular.js and many more web technologies. More...
 
 
Recommended Articles!
 
 
Search Articles
 
 
Subscribe & follow Us
 
 
 
Free ASP.NET MVC Interview Book
Learn In Hindi
Browse By Category
 
Recent Articles
 
Popular Articles
 
Like us on Facebook
 
Featured AD
Join our Training programs in Delhi/Noida on ASP.NET MVC, WCF, EF, jQuery - call us    +91-9871749695