dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.35k stars 9.99k forks source link

Can't get value by Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestHeaders.TryGetValue() #35064

Closed KamenRiderKuuga closed 3 years ago

KamenRiderKuuga commented 3 years ago

Describe the bug

When I want to get the header value from http request, I found something different from what I expected

To Reproduce

  1. Create a new ASP.NET Core Web API project.
  2. Add a global filter to the project.
  3. Send request with headers.
  4. Try to get header value from Request.Headers by TryGetValue()

Go with the sample project

  1. Get the sample project from:https://github.com/GadHao/ProjectForReproduce/tree/main/GetValueFromRequestHeader
  2. Run the project
  3. Send request with header. For example, using the Windows command line: curl -i -H "Accept: application/json" -H "one: 111" -H "two: 222" -H "three: 333" http://localhost:5000/weatherforecast
  4. See the output window

Exceptions (if any)

Can't get header value by headers.TryGetValue(key , out var value), but can get it from header[key]

If you go with the sample project, you will see:

header one :111
header two :
header three :
header one :111
header two :222
header three :333

Further technical details

.NET version: Reproduced in .NET Core 3.1 and .NET 5.0

adityamandaleeka commented 3 years ago

@GadHao Looks like you have some stray commas in your code :smile:

-            headers.TryGetValue("two,", out var two);
+            headers.TryGetValue("two", out var two);
KamenRiderKuuga commented 3 years ago

Embarrassed, I even spent a morning on it