@@ -13,6 +13,12 @@ namespace Microsoft.AspNetCore.Builder;
13
13
/// </summary>
14
14
public static class StaticFilesEndpointRouteBuilderExtensions
15
15
{
16
+ // By explicitly stating the supported HTTP methods for static files,
17
+ // we limit the types of situations where the fallback to file is matched
18
+ // after an endpoint is discarded, for example, due to a mismatched content type.
19
+ // See: https://github.com/dotnet/aspnetcore/issues/41060
20
+ private static readonly string [ ] _supportedHttpMethods = new [ ] { HttpMethods . Get , HttpMethods . Head } ;
21
+
16
22
/// <summary>
17
23
/// Adds a specialized <see cref="RouteEndpoint"/> to the <see cref="IEndpointRouteBuilder"/> that will match
18
24
/// requests for non-filenames with the lowest possible priority. The request will be routed to a
@@ -42,17 +48,12 @@ public static IEndpointConventionBuilder MapFallbackToFile(
42
48
this IEndpointRouteBuilder endpoints ,
43
49
string filePath )
44
50
{
45
- if ( endpoints == null )
46
- {
47
- throw new ArgumentNullException ( nameof ( endpoints ) ) ;
48
- }
51
+ ArgumentNullException . ThrowIfNull ( endpoints ) ;
52
+ ArgumentNullException . ThrowIfNull ( filePath ) ;
49
53
50
- if ( filePath == null )
51
- {
52
- throw new ArgumentNullException ( nameof ( filePath ) ) ;
53
- }
54
-
55
- return endpoints . MapFallback ( CreateRequestDelegate ( endpoints , filePath ) ) ;
54
+ return endpoints
55
+ . MapFallback ( CreateRequestDelegate ( endpoints , filePath ) )
56
+ . WithMetadata ( new HttpMethodMetadata ( _supportedHttpMethods ) ) ;
56
57
}
57
58
58
59
/// <summary>
@@ -83,17 +84,12 @@ public static IEndpointConventionBuilder MapFallbackToFile(
83
84
string filePath ,
84
85
StaticFileOptions options )
85
86
{
86
- if ( endpoints == null )
87
- {
88
- throw new ArgumentNullException ( nameof ( endpoints ) ) ;
89
- }
90
-
91
- if ( filePath == null )
92
- {
93
- throw new ArgumentNullException ( nameof ( filePath ) ) ;
94
- }
87
+ ArgumentNullException . ThrowIfNull ( endpoints ) ;
88
+ ArgumentNullException . ThrowIfNull ( filePath ) ;
95
89
96
- return endpoints . MapFallback ( CreateRequestDelegate ( endpoints , filePath , options ) ) ;
90
+ return endpoints
91
+ . MapFallback ( CreateRequestDelegate ( endpoints , filePath , options ) )
92
+ . WithMetadata ( new HttpMethodMetadata ( _supportedHttpMethods ) ) ;
97
93
}
98
94
99
95
/// <summary>
@@ -130,22 +126,13 @@ public static IEndpointConventionBuilder MapFallbackToFile(
130
126
[ StringSyntax ( "Route" ) ] string pattern ,
131
127
string filePath )
132
128
{
133
- if ( endpoints == null )
134
- {
135
- throw new ArgumentNullException ( nameof ( endpoints ) ) ;
136
- }
129
+ ArgumentNullException . ThrowIfNull ( endpoints ) ;
130
+ ArgumentNullException . ThrowIfNull ( pattern ) ;
131
+ ArgumentNullException . ThrowIfNull ( filePath ) ;
137
132
138
- if ( pattern == null )
139
- {
140
- throw new ArgumentNullException ( nameof ( filePath ) ) ;
141
- }
142
-
143
- if ( filePath == null )
144
- {
145
- throw new ArgumentNullException ( nameof ( filePath ) ) ;
146
- }
147
-
148
- return endpoints . MapFallback ( pattern , CreateRequestDelegate ( endpoints , filePath ) ) ;
133
+ return endpoints
134
+ . MapFallback ( pattern , CreateRequestDelegate ( endpoints , filePath ) )
135
+ . WithMetadata ( new HttpMethodMetadata ( _supportedHttpMethods ) ) ;
149
136
}
150
137
151
138
/// <summary>
@@ -181,22 +168,13 @@ public static IEndpointConventionBuilder MapFallbackToFile(
181
168
string filePath ,
182
169
StaticFileOptions options )
183
170
{
184
- if ( endpoints == null )
185
- {
186
- throw new ArgumentNullException ( nameof ( endpoints ) ) ;
187
- }
188
-
189
- if ( pattern == null )
190
- {
191
- throw new ArgumentNullException ( nameof ( filePath ) ) ;
192
- }
193
-
194
- if ( filePath == null )
195
- {
196
- throw new ArgumentNullException ( nameof ( filePath ) ) ;
197
- }
171
+ ArgumentNullException . ThrowIfNull ( endpoints ) ;
172
+ ArgumentNullException . ThrowIfNull ( pattern ) ;
173
+ ArgumentNullException . ThrowIfNull ( filePath ) ;
198
174
199
- return endpoints . MapFallback ( pattern , CreateRequestDelegate ( endpoints , filePath , options ) ) ;
175
+ return endpoints
176
+ . MapFallback ( pattern , CreateRequestDelegate ( endpoints , filePath , options ) )
177
+ . WithMetadata ( new HttpMethodMetadata ( _supportedHttpMethods ) ) ;
200
178
}
201
179
202
180
private static RequestDelegate CreateRequestDelegate (
0 commit comments