Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / OutputCacheAttribute.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.Diagnostics.CodeAnalysis;\r
16     using System.Web;\r
17     using System.Web.UI;\r
18 \r
19     [SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes",\r
20         Justification = "Unsealed so that subclassed types can set properties in the default constructor.")]\r
21     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]\r
22     public class OutputCacheAttribute : ActionFilterAttribute {\r
23 \r
24         private OutputCacheParameters _cacheSettings = new OutputCacheParameters();\r
25 \r
26         public string CacheProfile {\r
27             get {\r
28                 return _cacheSettings.CacheProfile ?? String.Empty;\r
29             }\r
30             set {\r
31                 _cacheSettings.CacheProfile = value;\r
32             }\r
33         }\r
34 \r
35         internal OutputCacheParameters CacheSettings {\r
36             get {\r
37                 return _cacheSettings;\r
38             }\r
39         }\r
40 \r
41         public int Duration {\r
42             get {\r
43                 return _cacheSettings.Duration;\r
44             }\r
45             set {\r
46                 _cacheSettings.Duration = value;\r
47             }\r
48         }\r
49 \r
50         public OutputCacheLocation Location {\r
51             get {\r
52                 return _cacheSettings.Location;\r
53             }\r
54             set {\r
55                 _cacheSettings.Location = value;\r
56             }\r
57         }\r
58 \r
59         public bool NoStore {\r
60             get {\r
61                 return _cacheSettings.NoStore;\r
62             }\r
63             set {\r
64                 _cacheSettings.NoStore = value;\r
65             }\r
66         }\r
67 \r
68         public string SqlDependency {\r
69             get {\r
70                 return _cacheSettings.SqlDependency ?? String.Empty;\r
71             }\r
72             set {\r
73                 _cacheSettings.SqlDependency = value;\r
74             }\r
75         }\r
76 \r
77         public string VaryByContentEncoding {\r
78             get {\r
79                 return _cacheSettings.VaryByContentEncoding ?? String.Empty;\r
80             }\r
81             set {\r
82                 _cacheSettings.VaryByContentEncoding = value;\r
83             }\r
84         }\r
85 \r
86         public string VaryByCustom {\r
87             get {\r
88                 return _cacheSettings.VaryByCustom ?? String.Empty;\r
89             }\r
90             set {\r
91                 _cacheSettings.VaryByCustom = value;\r
92             }\r
93         }\r
94 \r
95         public string VaryByHeader {\r
96             get {\r
97                 return _cacheSettings.VaryByHeader ?? String.Empty;\r
98             }\r
99             set {\r
100                 _cacheSettings.VaryByHeader = value;\r
101             }\r
102         }\r
103 \r
104         [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Param",\r
105             Justification = "Matches the @ OutputCache page directive.")]\r
106         public string VaryByParam {\r
107             get {\r
108                 return _cacheSettings.VaryByParam ?? String.Empty;\r
109             }\r
110             set {\r
111                 _cacheSettings.VaryByParam = value;\r
112             }\r
113         }\r
114 \r
115         public override void OnResultExecuting(ResultExecutingContext filterContext) {\r
116             if (filterContext == null) {\r
117                 throw new ArgumentNullException("filterContext");\r
118             }\r
119 \r
120             if (filterContext.IsChildAction) {\r
121                 return;\r
122             }\r
123 \r
124             // we need to call ProcessRequest() since there's no other way to set the Page.Response intrinsic\r
125             OutputCachedPage page = new OutputCachedPage(_cacheSettings);\r
126             page.ProcessRequest(HttpContext.Current);\r
127         }\r
128 \r
129         private sealed class OutputCachedPage : Page {\r
130             private OutputCacheParameters _cacheSettings;\r
131 \r
132             public OutputCachedPage(OutputCacheParameters cacheSettings) {\r
133                 // Tracing requires Page IDs to be unique.\r
134                 ID = Guid.NewGuid().ToString();\r
135                 _cacheSettings = cacheSettings;\r
136             }\r
137 \r
138             protected override void FrameworkInitialize() {\r
139                 // when you put the <%@ OutputCache %> directive on a page, the generated code calls InitOutputCache() from here\r
140                 base.FrameworkInitialize();\r
141                 InitOutputCache(_cacheSettings);\r
142             }\r
143         }\r
144 \r
145     }\r
146 }\r