Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Session is a disaster in ASP.NET MVC application?

Why is it being said We should not use Session variables in ASP.NET MVC applications ? I came across this answer which says so. In that case how will i maintain the values across requests like Logged in User information and some relevant data associated to his account?

This is Darin's answer.

Why are you using HttpContext.Current in an ASP.NET MVC application? Never use it. That's evil even in classic ASP.NET webforms applications but in ASP.NET MVC it's a disaster that takes all the fun out of this nice web framework.

like image 516
Happy Avatar asked Apr 16 '12 20:04

Happy


People also ask

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.

What can I use instead of session in ASP.NET MVC?

Use 'Local Storage 'or 'Session Storage'. Local Storage is not a replacement for Session State.

Which is better TempData or session in MVC?

TempData is ideal for short-lived things like displaying validation errors or other messages that we don't need to persist for longer than a single request. Session is ideal choice when we need to persist data for extended periods of time i.e. it is used to store data which is required throughout user session.

How long does session last MVC?

By default, session data is stored for 20 minutes from the last request. So, if you access a page and something is stored as session data for you, it will be automatically removed after 20 minutes unless you have made a new request within this time period.


1 Answers

One of the fundamental principles of frameworks like ASP.NET MVC is that they are stateless, just like the Web is. ASP.NET Web Forms is an attempt to mimic a stateful paradigm over a stateless enviroment. It is a lie, in other words.

Using Session variable in an ASP.NET MVC application is a bit like tying a horn to a horse's head, and calling it a Unicorn.

like image 184
Robert Harvey Avatar answered Oct 21 '22 10:10

Robert Harvey