Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

c# - How to extract custom header value?

I have this exact code from the accepted answer in my project which I need to migrate into ASP.NET Core MVP.

How to extract custom header value in Web API message handler?

var env = Request.Headers.GetValues("environment").First();

How can I implement this in .NET Core?

I hope this is not considered to be a duplicate question because I am trying to do this with the new system rather than the old one. I would also be fine if someone adds an answer to the link regarding the current version.

Edit: Where all types for http headers gone in ASP.NET 5? I tried this link but API may have changed. I don't think this is a duplicate for that question either.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...