-1

I am trying to fetch value of header from a request using this code

var headervalue= Request.Headers.GetValues("name")?.FirstOrDefault();

i thought this would also take care of the null value returned, as this might cause that issue

var headervalue= Request.Headers.GetValues("name").FirstOrDefault();

reason, is i want to avoid handling exception in case there is no header of specified key. in both above cases I get InvalidOperation Exception if header key is not found.

what is the best way to make this work without really having to handle invalidOp exception.

Update:

This particular syntax does not exist for me in my compiler

Request.Headers["name"] . hence GetValues() looks like my only option, which just throws invalidop exception if header isnt found.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Mandar Jogalekar
  • 3,199
  • 7
  • 44
  • 85
  • for me the request.headers[] syntax doesnt exist in compiler . looking for solution with GetValues() – Mandar Jogalekar Feb 07 '18 at 13:40
  • 1
    What is the type of `Request.Headers`? `HttpRequest.Headers` is a `NameValueCollection`. `HttpRequestMessage.Headers` is a `HttpRequestHeaders`. Only the latter has the `TryGetValues()` method. This may explain why the suggested duplicate has no answers referencing that method. So, please look at your tagging and question detail to make it clearer which types you are dealing with and whether this asp.net webforms, Web API, etc. – Stephen Kennedy Feb 07 '18 at 15:21
  • The OP stated that he was getting an exception with his line of code. Since `NameValueCollection.GetValues()` returns null if it can't find the requested value, but `HttpRequestHeaders.GetValues()` throws an exception, that implies that he's using the latter. – asherber Feb 08 '18 at 16:49
  • @asherber He also stated the problem was due to nulls. Perhaps we should vote to close the question as unclear. Btw do you have a source for HttpRequestMessage GetValues() throwing an exception if the header is not found? The MSDN docs are frustratingly silent on the matter. – Stephen Kennedy Feb 08 '18 at 16:58
  • 1
    @asherber https://github.com/dotnet/corefx/blob/3e72ee5971db5d0bd46606fa672969adde29e307/src/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs I found the source code for HttpHeaders as I couldn't construct one to test (no public ctor). You are right, it throws an InvalidOperationException if the key is not found. I had assumed it was a web forms request as `NameValueCollection.GetValues() ` (`Headers`) returns `null` if the key is not found. – Stephen Kennedy Feb 08 '18 at 17:15
  • @StephenKennedy You can construct a new `HttpRequestMessage` and then call `Headers.GetValues("foo")` to provoke the exception. – asherber Feb 08 '18 at 18:57
  • @asherber `new HttpRequestMessage().Headers.GetValues("foo")`. Confirmed exception. Cool trick, thanks for that. – Stephen Kennedy Feb 08 '18 at 19:01

1 Answers1

7

How about:

IEnumerable<string> values = null;
Request.Headers.TryGetValues("name", out values);
var headerValue = values?.FirstOrDefault();

In C#7, you can combine the first two lines:

Request.Headers.TryGetValues("name", out IEnumerable<string> values);
var headerValue = values?.FirstOrDefault();

Or:

string headerValue = null;
if (Request.Headers.Contains("name"))
    headervalue= Request.Headers.GetValues("name").FirstOrDefault();
asherber
  • 2,508
  • 1
  • 15
  • 12