Controlling Session Behavior in ASP.Net MVC

Introduction


ASP.NET MVC supports session state. As we know sessions are used to store data used across requests. ASP.NET MVC manages session data whether or not we store data in the session. Now with MVC, we can disable the session for the controller. This concept is called a sessionless controller.

Sessionless Controller


It is possible to disable the session for a controller. It may be that some of the controllers of our application do not use the session state feature. We can disable the session for those controllers using the SessionStateAttribute (set session state Behavior to Disabled) and we can get a slight performance improvement for our application.

Example 

  1. [SessionState(System.Web.SessionState.SessionStateBehavior. Disabled)]  
  2. public class HomeController : Controller  
  3. {  
  4.     ……  
  5.     ……  
  6. }

If we mark our controller as sessionless and we still try to access a session within the controller then it throws the exception “Object reference not set to an instance of an object”.

Example code

  1. [SessionState(System.Web.SessionState.SessionStateBehavior. Disabled)]  
  2. public class HomeController : Controller  
  3. {  
  4.     public ActionResult Index()  
  5.     {  
  6.         ViewBag.Message = "Welcome to ASP.NET MVC!";  
  7.         Session["test"] = "Session less controller test";  
  8.         return View();  
  9.     }  
  10. }

Output

Session less Controller

SessionState Behavior


The SessionState attribute in MVC provides more control over the behavior of the session state by specifying the value of the behavior property (this is a read-only property but we can set it using a parameterized contractor of the SessionStateAttribute).
 
Enumeration Value Description
Default The default ASP.NET logic is used to determine the session state behavior for the controller.
Required Full read and write session state behavior is enabled for the request.
ReadOnly Read-only session state is enabled for the request. In other words, we can read the data from the session but we cannot write the data into the session.
Disabled Session State is disabled for the current request.

Session less controller and TempData


TempData in MVC uses session state internally to store the data. So when we disable the session state for the controller, it will throw the exception as below.

  1. [SessionState(System.Web.SessionState.SessionStateBehavior.Disabled)]  
  2. public class HomeController : Controller  
  3. {   
  4.     public ActionResult Index()  
  5.     {  
  6.         ViewBag.Message = "Welcome to ASP.NET MVC!";  
  7.         TempData["test"] = "session less controller test";  
  8.         return View();  
  9.     }  
  10. }  

 

Output

Session less controller and TempData

Conclusion


The SessionState attribute specifies the session state behavior at the controller level. This attribute is a class level attribute so we cannot use it in a method (or at an action level). This attribute is very useful when we require specific behavior of session state for a specific controller. For example, for any controller, we do not want to use a session state; at this time we can mark this controller with the SessionState attribute and the behavior is disabled.


Similar Articles