Store complex object in TempData

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

How can I disable session state in ASP.NET MVC?

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 keep() vs peek()

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