Browse Tutorials

02 Intermediate

Controlling Session Behavior in Asp.Net MVC4

Controlling Session Behavior in Asp.Net MVC4

04 Apr 2024
Intermediate
195K Views
4 min read

Controlling Session: An Overview

By default, Asp.Net MVC support session state. The session is used to store data values across requests. Whether you store some data values within the session or not Asp.Net MVC must manage the session state for all the controllers in your application which is time-consuming. Since the session is stored on the server side and consumes server memory, hence it also affects your application performance. In this MVC Tutorial, we will explore more about Session state in MVC, disable session in mvc, session-less controller, and session-less action method. Consider our ASP.NET MVC Course for a better understanding of all MVC core concepts.

Session state in MVC

It provides more control over the behavior of the session state by specifying the value of the behavior property If it is a read-only property we can set it using a parameterized contractor of the SessionStateAttribute.

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 that controller and can gain slight performance improvement of your application. You can simplify the session state for your application by using available options for session state.

Session Less Controller

In Asp.Net MVC4, the SessionState attribute provides you more control over the behavior of the 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
The 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 uses a session state for storing the data values across requests. Hence, when you disable the session state for the controller, it will throw the exception as shown below:

Session Less Controller

Session-State Modes

  • In-Process Mode
  • State Server Mode
  • SQL Server Mode
  • Custom Mode

In-Process Mode

  • It is the default session state mode.
  • It is specified using the InProc SessionStateMode enumeration value.
  • It stores session state values.
  • Variables in memory on the local Web server.
  • It is the only mode that supports the Session_OnEnd event.
  • For more information about the Session_OnEnd event, see Session-State Events.

State Server Mode

  • It stores the session state in a process.
  • It is referred to as the ASP.NET state service, which is separate from the ASP.NET worker process or IIS application pool.
  • It ensures that the session state is preserved if the Web application is restarted
  • It makes the session state available to multiple Web servers in a Web farm.

SQL Server Mode

  • It stores the session state in an SQL Server database.
  • Using this mode ensures that the session state is preserved if the Web application is restarted.
  • It also makes the session state available to multiple Web servers in a Web farm.
Conclusion
So in this article, we have learned about controlling Session in asp.net mvc. I hope you enjoyed learning these concepts while programming with Asp.Net. Feel free to ask any questions from your side. Your valuable feedback or comments about this article are always welcome. Level up your career in MVC with our ASP.Net Core Certification.

FAQs

Q1. How to disable control level session state in MVC?

n Solution Explorer, double-click Web. config to view the contents of this file. Locate the <sessionState> section, and set the mode value to Off. Save the file and/or the project to disable session state throughout all pages in the application.

Q2. How are sessions maintained?

Sessions are maintained automatically by a session cookie that is sent to the client when the session is first created. 

Q3. What is session handling?

Session-handling customization is the process of manipulating server responses in such a way that application state information is preserved during load testing.
Share Article
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this