Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3317346
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:34:37+00:00 2026-05-17T22:34:37+00:00

In ASP.NET MVC 2, TempData values persist until the session ends or until they

  • 0

In ASP.NET MVC 2, TempData values persist until the session ends or until they are read. In the words of Microsoft…

The value of TempData persists until
it is read or until the session times
out. Persisting TempData in this way
enables scenarios such as redirection,
because the values in TempData are
available beyond a single request.

I thought I understood this but I just came across unusual behavior in my application where a TempData value was available and it should not have been available. In general, I have a controller with a series of actions where the first action sets a TempData value, the next few actions read and then set that TempData value, and the final action reads the TempData value. Pseudo code below…

[HttpPost]
public ActionResult Step1()
{
  TempData["bar"] = foo;
  return RedirectToAction("Step2");
}

public ActionResult Step2()
{
  var foo = TempData["bar"];
  TempData["bar"] = foo;
  return View();
}

[HttpPost]
public ActionResult Step2()
{
  var foo = TempData["bar"];
  TempData["bar"] = foo;
  return RedirectToAction("Step3");
}

public ActionResult Step3()
{
  var foo = TempData["bar"];
  TempData["bar"] = foo;
  return View();
}

[HttpPost]
public ActionResult Step3()
{
  var foo = TempData["bar"];
  return RedirectToAction("AnotherAction", "AnotherController");
}

My belief was that after reading a value, it would no longer be available in TempData. But I started stepping through the code and while the key/value would be added to TempData on assignment, it would never go away when I pulled the value from TempData (even when I arrived in a different controller).

The only way I am able to get it to go away is to manually hit an action that reads from TempData.

Can anyone provide any pointers to help me better understand what is going on with TempData persistence in ASP.NET MVC 2?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:34:37+00:00Added an answer on May 17, 2026 at 10:34 pm

    I’m going to throw this out there…

    RedirectToAction has a return type of RedirectToRouteResult. This is called by several of the action methods in the above pseudo code.

    According to this possibly outdated blog entry…

    4.RedirectResult and RedirectToRouteResult always calls
    TempData.Keep()

    and

    Calling Keep() from within an action
    method ensures that none of the items
    in TempData are removed at the end of
    the current request, even if they were
    read. The second overload can be used
    to retain specific items in TempData.

    So it looks like my TempData values are being automatically flagged. I verified this by seeing these values show up under _initialKeys within TempData.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.