Please enable Javascript to correctly display the contents on Dot Net Tricks!

Controlling Session Behavior in Asp.Net MVC4

  Posted By : Shailendra Chauhan, 06 Jan 2013
Updated On : 26 Sep 2016
Total Views : 157,574   
Support : MVC3 & MVC4
 

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.

 
You might like
 
5 OCT
ASP.NET MVC with AngularJS Development (online)

Mon-Fri 07:00 AM-08:30 AM IST

Know More
4 OCT
NodeJS Development (online)

Mon-Fri 08:00 PM - 09:30 PM IST

Know More
4 OCT
MEAN Stack Development (online)

MON-FRI 08:00 PM-09:30 PM IST

Know More
1 OCT
ASP.NET MVC with AngularJS Development (offline)

Sat-Sun 08:00 AM-09:30 AM

Know More
23 SEP
ASP.NET MVC with AngularJS Development (online)

MON-FRI     09:30 PM-11:00 PM IST

17 SEP
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     05:00 PM-06:30 PM IST

17 SEP
ASP.NET MVC with AngularJS Development (online)

Weekend (Sat, Sun)     03:00 PM-05:00 PM IST

17 SEP
PPC Marketing (offline)

Sat, Sun     03:00 PM-05:00 PM IST

3 SEP
NodeJS Development (offline)

Sat, Sun     11:00 AM-12:30 PM IST

24 JUL
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun     09:30 AM-11:00 AM

BROWSE BY CATEGORY
 
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+