Question

public ActionResult Index()
{
    TempData["msg"] = "Test";
    return RedirectToAction("About");
}

public ActionResult About()
{
    var msg = TempData["msg"];
    return View();
}

A simple question. I am sure I will slap my forehead when I see the answer to it.

Navigating to the Index action in the browser results in a redirect to the About action and the TempData value is correct.

Why when I navigate to the Index action using the Fiddler composer it results in a redirect to the About action but the TempData value is lost and null?

Was it helpful?

Solution

I think the answer is found here (http://msdn.microsoft.com/en-us/library/ms178581(v=vs.100).aspx):

"Sessions are identified by a unique identifier that can be read by using the SessionID property. When session state is enabled for an ASP.NET application, each request for a page in the application is examined for a SessionID value sent from the browser. If no SessionID value is supplied, ASP.NET starts a new session and the SessionID value for that session is sent to the browser with the response."

When I add this line to the beginning of each Action:

Debug.Write(string.Format("SessionId: {0}\r\n", HttpContext.Session.SessionID));

I see that when you run from a browser the sessionid is the same. When run from the Fiddler composer they are different.

Therefore, TempData is going to be reset using the default TempDataProvider (which stores the TempData in session state).

OTHER TIPS

If requests are the same than results should be the same. Most likely you are not making exact copy of first request when composing fake one. Note that in case of tempData your composed request will work (get tempData) only if it is the first request with this data - so you have to make "copy" of request that is not yet send by application, you can't replay ones that rely on tempData.

The temp data be stored in the session state and wiped out after first request, so as result it will be invalid/missing if you are not correctly sending it information hand/via Fiddler composer OR (as in your case) making second request with the same information to the same controller.

See also other related questions on the same topic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top