Why is HttpContext Header Empty when trying to retrieve Header Information in MVC



Question: HttpContext Header is empty when attempt to retrieve data in C#, why is that?

Answer: There is a chance that you are trying to retrieve the Keys from the HttpContext Header in a wrong way. Keep in mind that HttpContext Header Keys shows differently when you step in the HttpContext using a Debugger, however, when actually quering the Values you need to drop the "Header" section. For Example:

 

var UserAgent = httpContext.Request.Header["HeaderUserAgent"] //This is wrong and will not yeld any results
var UserAgent = httpContext.Request.Header["User-Agent"] //This is right, will yeld results.

- Hope this helps you.

HttpContext not getting values Asp.Net 5 HttpContext get headers from HttpContext .NET 6 published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback

Other users would like to know if this solution helped you.

Your Session is Ending
Login to Continue


© 2021 - ErnesTech - Privacy