Using TempData while <sessionState mode="Off" />
Asked Answered
A

1

11

I was trying to make one site as session less. So add <sessionState mode="Off" /> to my web.config. After that if I execute

 Session["test"] = "yes"; 

I get an error “Object reference not set to an instance of an object.” This is fine.

But with this configuration I can set

TempData["test"] = "yes"; 

in controller and print TempData["test"]; in View page. It is working well.

As per MSDN “The TempData property value is stored in session state”.

So if I mention <sessionState mode="Off" /> how TempData is working? Is ASP.NET still storing TempData in session or somewhere else?

Approbation answered 27/5, 2012 at 8:55 Comment(0)
C
18

This can work with disabled session state only if you are printing TempData["test"] on your View during the same request - thus this value is removed from the TempData dictionary and MVC is not trying to save it to the session state. Try to remove it from your view and you'll get System.InvalidOperationException: The SessionStateTempDataProvider class requires session state to be enabled.

You can individually disable session state for your controllers by adding [SessionState(SessionStateBehavior.Disabled)] attribute or implementing your own TempData provider using ITempDataProvider interface.

[EDIT] Just to explain my first point, let's imagine two situations:

  • You have controller action that assigns some value to TempData["test"] and a corresponding view that is being returned from this action and prints this data. In this case everything will be working even without a session state because there is no need to store TempData["test"] between requests.
  • You have controller action that assigns some value to TempData["test"] but redirects user to another action with its own view that prints this data. In this case it will not work with session state disabled as MVC needs to store your TempData["test"] value between two requests.
Confabulate answered 27/5, 2012 at 9:15 Comment(4)
I understand about ITempDataProvider and thanks for that. I also found that instead of session you can keep temp data in cookies from here. afana.me/post/….Approbation
Yes, the guy has implemented his own TempData provider that is based on cookies.Confabulate
We use TempData for single request right? So we set value for TempData in controller and get in View page. I have one doubt left about “Try to remove it from your view and you'll get error”. Can you please explain that?Approbation
@JomyJohn I think what you need is not TempData if you're reading it in the same request (the view rendered by the controller that stored tempdata). You might want to use ViewData or ViewBag collection. TempData is mostly for storing data that will be read in a different request, e.g.: if your controller does a RedirectToAction() from request #1...that sends a HTTP 302 response back to the browser which then redirects (request #2) to server. At this point your ViewBag/ViewData is empty, but TempData still holds data in Session until you read it. This is also the case for AJAX requestsDannettedanni

© 2022 - 2024 — McMap. All rights reserved.