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.Diagnostics.CodeAnalysis;
\r
17 using System.Web.UI;
\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
24 private OutputCacheParameters _cacheSettings = new OutputCacheParameters();
\r
26 public string CacheProfile {
\r
28 return _cacheSettings.CacheProfile ?? String.Empty;
\r
31 _cacheSettings.CacheProfile = value;
\r
35 internal OutputCacheParameters CacheSettings {
\r
37 return _cacheSettings;
\r
41 public int Duration {
\r
43 return _cacheSettings.Duration;
\r
46 _cacheSettings.Duration = value;
\r
50 public OutputCacheLocation Location {
\r
52 return _cacheSettings.Location;
\r
55 _cacheSettings.Location = value;
\r
59 public bool NoStore {
\r
61 return _cacheSettings.NoStore;
\r
64 _cacheSettings.NoStore = value;
\r
68 public string SqlDependency {
\r
70 return _cacheSettings.SqlDependency ?? String.Empty;
\r
73 _cacheSettings.SqlDependency = value;
\r
77 public string VaryByContentEncoding {
\r
79 return _cacheSettings.VaryByContentEncoding ?? String.Empty;
\r
82 _cacheSettings.VaryByContentEncoding = value;
\r
86 public string VaryByCustom {
\r
88 return _cacheSettings.VaryByCustom ?? String.Empty;
\r
91 _cacheSettings.VaryByCustom = value;
\r
95 public string VaryByHeader {
\r
97 return _cacheSettings.VaryByHeader ?? String.Empty;
\r
100 _cacheSettings.VaryByHeader = value;
\r
104 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Param",
\r
105 Justification = "Matches the @ OutputCache page directive.")]
\r
106 public string VaryByParam {
\r
108 return _cacheSettings.VaryByParam ?? String.Empty;
\r
111 _cacheSettings.VaryByParam = value;
\r
115 public override void OnResultExecuting(ResultExecutingContext filterContext) {
\r
116 if (filterContext == null) {
\r
117 throw new ArgumentNullException("filterContext");
\r
120 if (filterContext.IsChildAction) {
\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
129 private sealed class OutputCachedPage : Page {
\r
130 private OutputCacheParameters _cacheSettings;
\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
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