1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections.Generic;
\r
16 using System.Web.Mvc.Resources;
\r
18 public class SessionStateTempDataProvider : ITempDataProvider {
\r
19 internal const string TempDataSessionStateKey = "__ControllerTempData";
\r
21 public virtual IDictionary<string, object> LoadTempData(ControllerContext controllerContext) {
\r
22 HttpSessionStateBase session = controllerContext.HttpContext.Session;
\r
24 if (session != null) {
\r
25 Dictionary<string, object> tempDataDictionary = session[TempDataSessionStateKey] as Dictionary<string, object>;
\r
27 if (tempDataDictionary != null) {
\r
28 // If we got it from Session, remove it so that no other request gets it
\r
29 session.Remove(TempDataSessionStateKey);
\r
30 return tempDataDictionary;
\r
34 return new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
\r
37 public virtual void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values) {
\r
38 if (controllerContext == null) {
\r
39 throw new ArgumentNullException("controllerContext");
\r
42 HttpSessionStateBase session = controllerContext.HttpContext.Session;
\r
43 bool isDirty = (values != null && values.Count > 0);
\r
45 if (session == null) {
\r
47 throw new InvalidOperationException(MvcResources.SessionStateTempDataProvider_SessionStateDisabled);
\r
52 session[TempDataSessionStateKey] = values;
\r
55 // Since the default implementation of Remove() (from SessionStateItemCollection) dirties the
\r
56 // collection, we shouldn't call it unless we really do need to remove the existing key.
\r
57 if (session[TempDataSessionStateKey] != null) {
\r
58 session.Remove(TempDataSessionStateKey);
\r