Merge pull request #569 from knocte/fix_cairo_profile_versions_problem
[mono.git] / mcs / class / System.Web.Mvc / 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             HttpContextBase httpContext = controllerContext.HttpContext;\r
23             \r
24             if (httpContext.Session == null) {\r
25                 throw new InvalidOperationException(MvcResources.SessionStateTempDataProvider_SessionStateDisabled);\r
26             }\r
27 \r
28             Dictionary<string, object> tempDataDictionary = httpContext.Session[TempDataSessionStateKey] as Dictionary<string, object>;\r
29 \r
30             if (tempDataDictionary != null) {\r
31                 // If we got it from Session, remove it so that no other request gets it\r
32                 httpContext.Session.Remove(TempDataSessionStateKey);\r
33                 return tempDataDictionary;\r
34             }\r
35             else {\r
36                 return new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);\r
37             }\r
38         }\r
39 \r
40         public virtual void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values) {\r
41             HttpContextBase httpContext = controllerContext.HttpContext;\r
42 \r
43             if (httpContext.Session == null) {\r
44                 throw new InvalidOperationException(MvcResources.SessionStateTempDataProvider_SessionStateDisabled);\r
45             }\r
46 \r
47             httpContext.Session[TempDataSessionStateKey] = values;\r
48         }        \r
49     }\r
50 }\r