Is it possible to access the TempData key/value from HttpContext?
var foo = filterContext.Controller.TempData[“foo”];
var foo = filterContext.Controller.TempData[“foo”];
TempData uses Session, which itself uses IDistributedCache. IDistributedCache doesn’t have the capability to accept objects or to serialize objects. As a result, you need to do this yourself, i.e.: TempData[“PopupMessages”] = JsonConvert.SerializeObject(_popupMessages); Then, of course, after redirecting, you’ll need to deserialize it back into the object you need: TempData[“PopupMessages”] = JsonConvert.DeserializeObject<List<PopupMessage>>(TempData[“PopupMessages”].ToString());
TempData is session, so they’re not entirely different. However, the distinction is easy to understand, because TempData is for redirects, and redirects only. So when you set some message in TempData and then redirect, you are using TempData correctly. However, using Session for any kind of security is extremely dangerous. Session and Membership are entirely … Read more
You could make your own ControllerFactory and DummyTempDataProvider. Something like this: public class NoSessionControllerFactory : DefaultControllerFactory { protected override IController GetControllerInstance(Type controllerType) { var controller = base.GetControllerInstance(controllerType); ((Controller) controller).TempDataProvider = new DummyTempDataProvider(); return controller; } } public class DummyTempDataProvider : ITempDataProvider { public IDictionary<string, object> LoadTempData(ControllerContext controllerContext) { return new Dictionary<string, object>(); } public void … Read more
TempData is a bucket where you can dump data that is only needed for the following request. That is, anything you put into TempData is discarded after the next request completes. This is useful for one-time messages, such as form validation errors. The important thing to take note of here is that this applies to … Read more
You can create the extension methods like this: public static class TempDataExtensions { public static void Put<T>(this ITempDataDictionary tempData, string key, T value) where T : class { tempData[key] = JsonConvert.SerializeObject(value); } public static T Get<T>(this ITempDataDictionary tempData, string key) where T : class { object o; tempData.TryGetValue(key, out o); return o == null ? … Read more
When an object in a TempDataDictionary is read, it will be marked for deletion at the end of that request. That means if you put something on TempData like TempData[“value”] = “someValueForNextRequest”; And on another request you access it, the value will be there but as soon as you read it, the value will be … Read more
No need to have an aversion to TempData… But if not used correctly it could surely be an indication of poor design. If you are using RESTful URL’s, TempData is a best practice for transfering messages from your POST Actions to your GET Actions. Consider this: You have a form at URL Products/New. The form … Read more