Make GetHeaderValue support HttpContext unavailable (#16654)

* Make GetHeaderValue tolerant for when the http context is not available. Now it just returns null.

* Add unit tests
This commit is contained in:
Bjarke Berg
2024-06-25 13:47:25 +02:00
committed by GitHub
parent e3d65967aa
commit 0afb4f7283
2 changed files with 49 additions and 7 deletions

View File

@@ -8,11 +8,5 @@ internal abstract class RequestHeaderHandler
protected RequestHeaderHandler(IHttpContextAccessor httpContextAccessor) => _httpContextAccessor = httpContextAccessor;
protected string? GetHeaderValue(string headerName)
{
HttpContext httpContext = _httpContextAccessor.HttpContext ??
throw new InvalidOperationException("Could not obtain an HTTP context");
return httpContext.Request.Headers[headerName];
}
protected string? GetHeaderValue(string headerName) => _httpContextAccessor.HttpContext?.Request.Headers[headerName];
}