File tree Expand file tree Collapse file tree 2 files changed +22
-2
lines changed
src/Middleware/OutputCaching Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -66,12 +66,18 @@ internal IOutputCachePolicy BuildPolicy()
66
66
return _builtPolicy ;
67
67
}
68
68
69
- var builder = new OutputCachePolicyBuilder ( ) ;
69
+ OutputCachePolicyBuilder builder ;
70
70
71
71
if ( PolicyName != null )
72
72
{
73
+ // Don't add the default policy if a named one is used as it could already contain it
74
+ builder = new OutputCachePolicyBuilder ( excludeDefaultPolicy : true ) ;
73
75
builder . AddPolicy ( new NamedPolicy ( PolicyName ) ) ;
74
76
}
77
+ else
78
+ {
79
+ builder = new ( ) ;
80
+ }
75
81
76
82
if ( _noCache != null && _noCache . Value )
77
83
{
Original file line number Diff line number Diff line change @@ -48,7 +48,7 @@ public async Task Attribute_CreatesNamedPolicy()
48
48
var options = new OutputCacheOptions ( ) ;
49
49
options . AddPolicy ( "MyPolicy" , b => b . Expire ( TimeSpan . FromSeconds ( 42 ) ) ) ;
50
50
51
- var context = TestUtils . CreateTestContext ( options : options ) ;
51
+ var context = TestUtils . CreateUninitializedContext ( options : options ) ;
52
52
53
53
var attribute = OutputCacheMethods . GetAttribute ( nameof ( OutputCacheMethods . PolicyName ) ) ;
54
54
await attribute . BuildPolicy ( ) . CacheRequestAsync ( context , cancellation : default ) ;
@@ -57,6 +57,20 @@ public async Task Attribute_CreatesNamedPolicy()
57
57
Assert . Equal ( 42 , context . ResponseExpirationTimeSpan ? . TotalSeconds ) ;
58
58
}
59
59
60
+ [ Fact ]
61
+ public async Task Attribute_NamedPolicyDoesNotInjectDefaultPolicy ( )
62
+ {
63
+ var options = new OutputCacheOptions ( ) ;
64
+ options . AddPolicy ( "MyPolicy" , b => b . With ( x => false ) . Cache ( ) ) ;
65
+
66
+ var context = TestUtils . CreateUninitializedContext ( options : options ) ;
67
+
68
+ var attribute = OutputCacheMethods . GetAttribute ( nameof ( OutputCacheMethods . PolicyName ) ) ;
69
+ await attribute . BuildPolicy ( ) . CacheRequestAsync ( context , cancellation : default ) ;
70
+
71
+ Assert . False ( context . EnableOutputCaching ) ;
72
+ }
73
+
60
74
[ Fact ]
61
75
public async Task Attribute_CreatesVaryByHeaderPolicy ( )
62
76
{
You can’t perform that action at this time.
0 commit comments