Why does the IEnumerable returned by HttpRequestHeaders.GetValues ​​(string) only have 1 string?

I can't figure out why the IEnumerable returned by HttpRequestHeaders.GetValues ​​(string) returns an IEnumerable that only has one element in it.

When the header contains multiple elements with the requested name, I get an IEnumerable with 1 string entry, which is equal to the CSAT concatenation of all matching header elements.

I would expect, if the return value is IEnumerable, that each matching header entry would be its own element. Conversely, if a method always concatenates everything, then I expect the return value to be just a single line.

Expected:

{ "Test", "Test2" }

      

Actual:

{ "Test, Test2" }

      

Using Fiddler for testing purposes, I tried to add multiple entries to my header:

GET https://localhost/api/v1/users/1 HTTP/1.1
Host: localhost
TestHeaderField: Test
TestHeaderField: Test2

      

Like multiple comma separated items:

GET https://localhost/api/v1/users/1 HTTP/1.1
Host: localhost
TestHeaderField: Test, Test2

      

I am using ASP.NET Web API 2 version 5.0.0, with .Net 4.5.1. Is there something I am missing here or is this a bug in the underlying code?

+3


source to share


2 answers


HttpRequestHeaders.GetValues ​​will return multiple values ​​for headers for which it has parsers, if the values ​​are well formed. You can check this by sending some headers Accept

:



GET https://localhost/api/v1/users/1 HTTP/1.1
Host: localhost
Accept: text/html
Accept: application/xhtml+xml
Accept: application/xml

      

0


source


You can try something like this to get each request header:

NameValueCollection headers = HttpRequestHeaders;
for (int i = 0; i < headers.Count; i++)
{
    string key = headers.GetKey(i);
    string value = headers.Get(i);
    // use key and value
}

      



Change this if needed, you only have values, not keys.

0


source







All Articles