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
- [SessionState(System.Web.SessionState.SessionStateBehavior. Disabled)]
- public class HomeController : Controller
- {
- ……
- ……
- }
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
- [SessionState(System.Web.SessionState.SessionStateBehavior. Disabled)]
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- ViewBag.Message = "Welcome to ASP.NET MVC!";
- Session["test"] = "Session less controller test";
- return View();
- }
- }
Output

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).


Pranil KadamPosted Apr 20, 2018, 8:34 AM
HI tried this demo but i did't get error in case of tempdata when session state is disabled. please let me know reason
Umesh ApPosted Aug 24, 2016, 2:27 AM
Good info......