How do you obtain a strongly typed header class from the namespace System.Net.Http.Headers
from an ASP.NET Core controller? In a controller derived from Controller
, Request.Headers
is available, but it just returns IHeaderDictionary
. There is also an extension method HeaderDictionaryTypeExtensions.GetTypedHeaders
, but it returns RequestHeaders
, which has only certain headers. The class HttpRequestHeaders
has the most comprehensive list of headers, but it's not clear how to access it.
For example, how would you get a AuthenticationHeaderValue
? One option is AuthenticationHeaderValue.Parse(Request.Headers["Authorization"])
, but that requires hard coding the header name. Perhaps there is a non-hard-coded way to get to HttpRequestHeaders.Authorization
.
RequestHeaders
, which is useful for some headers, but not others. This question focuses on how to get the more comprehensiveHttpRequestHeaders
available in ASP.NET Core 1.0. – Involution