Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / SessionStateTempDataProvider.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\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
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.Web.Mvc.Resources;\r
17 \r
18     public class SessionStateTempDataProvider : ITempDataProvider {\r
19         internal const string TempDataSessionStateKey = "__ControllerTempData";\r
20 \r
21         public virtual IDictionary<string, object> LoadTempData(ControllerContext controllerContext) {\r
22             HttpSessionStateBase session = controllerContext.HttpContext.Session;\r
23 \r
24             if (session != null) {\r
25                 Dictionary<string, object> tempDataDictionary = session[TempDataSessionStateKey] as Dictionary<string, object>;\r
26 \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
31                 }\r
32             }\r
33 \r
34             return new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);\r
35         }\r
36 \r
37         public virtual void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values) {\r
38             if (controllerContext == null) {\r
39                 throw new ArgumentNullException("controllerContext");\r
40             }\r
41 \r
42             HttpSessionStateBase session = controllerContext.HttpContext.Session;\r
43             bool isDirty = (values != null && values.Count > 0);\r
44 \r
45             if (session == null) {\r
46                 if (isDirty) {\r
47                     throw new InvalidOperationException(MvcResources.SessionStateTempDataProvider_SessionStateDisabled);\r
48                 }\r
49             }\r
50             else {\r
51                 if (isDirty) {\r
52                     session[TempDataSessionStateKey] = values;\r
53                 }\r
54                 else {\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
59                     }\r
60                 }\r
61             }\r
62         }\r
63 \r
64     }\r
65 }\r