2010-06-21 Marek Habersack <mhabersack@novell.com>
[mcs.git] / class / System.Web.Mvc / System.Web.Mvc / SessionStateTempDataProvider.cs
blob427922cba5dee6f24d83742c0aaa8b90564ae6bd
1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation. All rights reserved.
5 * This software is subject to the Microsoft Public License (Ms-PL).
6 * A copy of the license can be found in the license.htm file included
7 * in this distribution.
9 * You must not remove this notice, or any other, from this software.
11 * ***************************************************************************/
13 namespace System.Web.Mvc {
14 using System;
15 using System.Collections.Generic;
16 using System.Web.Mvc.Resources;
18 public class SessionStateTempDataProvider : ITempDataProvider {
19 internal const string TempDataSessionStateKey = "__ControllerTempData";
21 public virtual IDictionary<string, object> LoadTempData(ControllerContext controllerContext) {
22 HttpContextBase httpContext = controllerContext.HttpContext;
24 if (httpContext.Session == null) {
25 throw new InvalidOperationException(MvcResources.SessionStateTempDataProvider_SessionStateDisabled);
28 Dictionary<string, object> tempDataDictionary = httpContext.Session[TempDataSessionStateKey] as Dictionary<string, object>;
30 if (tempDataDictionary != null) {
31 // If we got it from Session, remove it so that no other request gets it
32 httpContext.Session.Remove(TempDataSessionStateKey);
33 return tempDataDictionary;
35 else {
36 return new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
40 public virtual void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values) {
41 HttpContextBase httpContext = controllerContext.HttpContext;
43 if (httpContext.Session == null) {
44 throw new InvalidOperationException(MvcResources.SessionStateTempDataProvider_SessionStateDisabled);
47 httpContext.Session[TempDataSessionStateKey] = values;