How to extract custom header value? How to extract custom header value? asp.net asp.net

How to extract custom header value?


Request.Headers returns Microsoft.AspNetCore.Http.IHeaderDictionary interface that define next property:

StringValues this[string key] { get; set; }

IHeaderDictionary has a different indexer contract than IDictionary, where it will return StringValues.Empty for missing entries.

Return type: Microsoft.Extensions.Primitives.StringValues

Returns: The stored value, or StringValues.Empty if the key is not present.

So, you can simply use Request.Headers["environment"] to get value of "environment" header


Since this answer is viewed quite a lot, you can also access this fields by using attributes in the parameters. Here is an example:

enter image description here

In the request you can access any fields in the body, queryString or headers. Here is the Postman example regarding the authorization header:

enter image description here