Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would ASP.NET MVC use session state?

Recommended by the ASP.NET team to use cache instead of session, we stopped using session from working with the WebForm model the last few years. So we normally have the session turned off in the web.config

<sessionState mode="Off" /> 

But, now when I'm testing out a ASP.NET MVC application with this setting it throws an error in class SessionStateTempDataProvider inside the mvc framework, it asked me to turn on session state, I did and it worked. Looking at the source it uses session:

// line 20 in SessionStateTempDataProvider.cs Dictionary<string, object> tempDataDictionary =  httpContext.Session[TempDataSessionStateKey] as Dictionary<string, object>;  

So, why would they use session here? What am I missing?

========================================================

Edit Sorry didn't mean for this post to debate on session vs. cache, but rather in the context of the ASP.NET MVC, I was just wondering why session is used here. In this blog post also Scott Watermasysk mentioned that turning off session is a good practice, so I'm just wondering why I have to turn it on to use MVC from here on.

like image 489
Ray Avatar asked Dec 22 '08 19:12

Ray


People also ask

Why do we use session state?

ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request.

Why session is used in MVC?

ASP.NET MVC Session state enables you to store and retrieve values for a user when the user navigatesto other view in an ASP.NET MVC application.

What is session state in ASP.NET MVC?

Session state is an ASP.NET Core scenario for storage of user data while the user browses a web app. Session state uses a store maintained by the app to persist data across requests from a client.

Is it good to use session in MVC?

It is perfectly OK to use sessions in ASP.NET MVC, especially in the shopping cart scenario of yours.


2 Answers

Session is used for the TempData store. TempData is a highly limited form of session state which will last only until the next request from a certain user. (Edit In MVC 2+, it lasts until it is next read.) The purpose of TempData is to store data, then do a redirect, and have the stored data be available to the action to which you just redirected.

Using Session for the TempData store means that any distributed caching system which already handles Session will work for TempData. Avoiding using Session directly when TempData will do has a couple of advantages. One is that you don't have to clean up the Session yourself; TempData will "expire" on its own.

like image 71
Craig Stuntz Avatar answered Sep 22 '22 22:09

Craig Stuntz


Recommended by the ASP.NET team to use cache instead of session

@ray247, could you provide a reference for this? Session and Cache are different by nature and should be used depending on application requirements. For example storing user specific data into the cache could lead to undesired behavior. Of course if you really want to avoid using session you could provide your own implementation of the ITempDataProvider interface.

like image 22
Darin Dimitrov Avatar answered Sep 22 '22 22:09

Darin Dimitrov