Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Controlling Session Behavior in Asp.Net MVC4

Controlling Session Behavior in Asp.Net MVC4

04 Apr 2024
Intermediate
197K Views
4 min read
Learn with an interactive course and practical hands-on labs

ASP.NET MVC with Web API Foundations Course - Free

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

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.

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

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
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this