Use Session in MVC or not?

Camillia
Camillia used Ask the Experts™
on
I'm reviewing this junior developer's code and he's used Session. I think there's a better way of doing this and not use Session. I also read somewhere, a while back, not to use Session.

He doesn't clear the session.

  if (search.Schedule.Invoices.Any())
            {
                Session["leaseSched"] = search.Schedule.LeaseNoSchedule;
            }

and then in a link

<div class="btn-group pull-right">
    <h5>@Display <a href="@Url.Action("Detail", "Schedule", new { @id = Model.ScheduleID })">@Session["leaseSched"].ToString()</a></h5>
</div>

Any arguments against using Session in MVC?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior .Net Developer
Commented:
It depends on what you're using it for.

If it's only for redirects, you can get away with TempData.

A good place for learning the differences between TempData and session:
https://www.c-sharpcorner.com/blogs/tempdata-vs-session-in-mvc1
Thanks, Kyle. I think TempData is better.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
If it's only for redirects, you can get away with TempData.
TempData actually uses session under the hood.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Ha, didnt know. The link Kyle posted lists the differences.
I've been reading about use of session in mvc . Looks like session is not the best practice.
Kyle AbrahamsSenior .Net Developer

Commented:
TempData actually uses session under the hood.

Yes, but only for the redirect.  Normal session keeps it live even after the page is loaded.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Agreed, but I mention it because that means if you use TempData, then you can't disable session for your app.
Thanks, Kauf. Makes sense. I thought you meant there's no difference between using session or tempdata.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial