From 57c8b71b4dae98f969f43a0386c14422fb1f2a07 Mon Sep 17 00:00:00 2001 From: Javier Calvarro Nelson Date: Fri, 29 Dec 2017 18:56:22 -0800 Subject: [PATCH] [Fixes #6547] Re-introduce HttpMethodActionConstraint dictionary perf improvement --- .../Internal/CorsHttpMethodActionConstraint.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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)) {