diff --git a/src/Microsoft.AspNetCore.Mvc.Cors/Internal/CorsHttpMethodActionConstraint.cs b/src/Microsoft.AspNetCore.Mvc.Cors/Internal/CorsHttpMethodActionConstraint.cs index 8b5c6f57cb..71ba2d696c 100644 --- a/src/Microsoft.AspNetCore.Mvc.Cors/Internal/CorsHttpMethodActionConstraint.cs +++ b/src/Microsoft.AspNetCore.Mvc.Cors/Internal/CorsHttpMethodActionConstraint.cs @@ -35,8 +35,9 @@ namespace Microsoft.AspNetCore.Mvc.Cors.Internal } var request = context.RouteContext.HttpContext.Request; - if (request.Headers.ContainsKey(OriginHeader) && - string.Equals(request.Method, PreflightHttpMethod, StringComparison.OrdinalIgnoreCase) && + // Perf: Check http method before accessing the Headers collection. + if (string.Equals(request.Method, PreflightHttpMethod, StringComparison.OrdinalIgnoreCase) && + request.Headers.ContainsKey(OriginHeader) && request.Headers.TryGetValue(AccessControlRequestMethod, out var accessControlRequestMethod) && !StringValues.IsNullOrEmpty(accessControlRequestMethod)) {