|
3 | 3 | using Microsoft.AspNetCore.Http; |
4 | 4 | using Microsoft.AspNetCore.Http.Extensions; |
5 | 5 | using Microsoft.AspNetCore.Mvc.Abstractions; |
6 | | - using Microsoft.AspNetCore.Mvc.ActionConstraints; |
7 | 6 | using Microsoft.AspNetCore.Mvc.Infrastructure; |
8 | 7 | using Microsoft.AspNetCore.Mvc.Internal; |
9 | 8 | using Microsoft.AspNetCore.Routing; |
|
16 | 15 | using Versioning; |
17 | 16 | using static ApiVersion; |
18 | 17 | using static System.Environment; |
| 18 | + using static System.Linq.Enumerable; |
19 | 19 | using static System.String; |
20 | 20 | using static Versioning.ErrorCodes; |
21 | 21 |
|
@@ -193,10 +193,7 @@ RequestHandler ClientError( RouteContext context, ActionSelectionResult selectio |
193 | 193 | var requestedVersion = default( string ); |
194 | 194 | var parsedVersion = properties.ApiVersion; |
195 | 195 | var actionNames = new Lazy<string>( () => Join( NewLine, candidates.Select( a => a.DisplayName ) ) ); |
196 | | - var allowedMethods = new Lazy<HashSet<string>>( |
197 | | - () => new HashSet<string>( candidates.SelectMany( c => c.ActionConstraints.OfType<HttpMethodActionConstraint>() ) |
198 | | - .SelectMany( ac => ac.HttpMethods ), |
199 | | - StringComparer.OrdinalIgnoreCase ) ); |
| 196 | + var allowedMethods = new Lazy<HashSet<string>>( () => AllowedMethodsFromCandidates( candidates ) ); |
200 | 197 | var newRequestHandler = default( Func<IErrorResponseProvider, string, string, RequestHandler> ); |
201 | 198 |
|
202 | 199 | if ( parsedVersion == null ) |
@@ -250,6 +247,29 @@ RequestHandler ClientError( RouteContext context, ActionSelectionResult selectio |
250 | 247 | return newRequestHandler( ErrorResponseProvider, code, message ); |
251 | 248 | } |
252 | 249 |
|
| 250 | + static HashSet<string> AllowedMethodsFromCandidates( IEnumerable<ActionDescriptor> candidates ) |
| 251 | + { |
| 252 | + Contract.Requires( candidates != null ); |
| 253 | + Contract.Ensures( Contract.Result<HashSet<string>>() != null ); |
| 254 | + |
| 255 | + var httpMethods = new HashSet<string>( StringComparer.OrdinalIgnoreCase ); |
| 256 | + |
| 257 | + foreach ( var candidate in candidates ) |
| 258 | + { |
| 259 | + if ( candidate.ActionConstraints == null ) |
| 260 | + { |
| 261 | + continue; |
| 262 | + } |
| 263 | + |
| 264 | + foreach ( var constraint in candidate.ActionConstraints.OfType<HttpMethodActionConstraint>() ) |
| 265 | + { |
| 266 | + httpMethods.AddRange( constraint.HttpMethods ); |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + return httpMethods; |
| 271 | + } |
| 272 | + |
253 | 273 | sealed class DefaultActionHandler |
254 | 274 | { |
255 | 275 | readonly IActionContextAccessor actionContextAccessor; |
|
0 commit comments