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