ASP.NET MVC and TempData

March 17, 2013

TempData initially seemed kind of magical and uber useful!

Represents a set of data that persists only from one request to the next.

Today I almost used it in a multi-instance Azure set up which didn’t use session state and so didn’t have a central session store. Whoops.

Why was that bad?

By default TempData is stored in session state, which means if the redirected request didn’t hit the same instance then it would look like TempData was simply not set.

But what if I still want to use it?

You can specify an alternate TempData provider and store it somewhere else. Greg Shackles has a great post here with all the details.

I learned a valuable lesson today

Always thoroughly read the documentation and know the details.

This potential bug wouldn’t have presented itself on my development machine and would have resulted in some seemly random behaviour in stage.