如何提取自定义标头值? [英] How to extract custom header value?

查看:124
本文介绍了如何提取自定义标头值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从项目中的可接受答案中获得了确切的代码,需要将其迁移到ASP.NET Core MVP中。

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

如何在Web API消息处理程序中提取自定义标头值?

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

如何在.NET Core中实现呢?

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.

编辑: ASP.NET 5中HTTP标头的所有类型都在哪里?我尝试了此链接,但API可能具有改变了。我也不认为这是该问题的重复项。

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.

推荐答案

Request.Headers返回定义了Microsoft.AspNetCore.Http.IHeaderDictionary 接口下一个属性:

Request.Headers returns Microsoft.AspNetCore.Http.IHeaderDictionary interface that define next property:

StringValues this[string key] { get; set; }




IHeaderDictionary与IDictionary具有不同的索引器合约,它将在其中返回StringValues.Empty缺少条目。

IHeaderDictionary has a different indexer contract than IDictionary, where it will return StringValues.Empty for missing entries.

返回类型:Microsoft.Extensions.Primitives.StringValues

Return type: Microsoft.Extensions.Primitives.StringValues

返回:存储值或StringValues.Empty(如果键不存在)。

Returns: The stored value, or StringValues.Empty if the key is not present.

因此,您可以简单地使用 Request .Headers [ environment] 获取 environment标题的值

So, you can simply use Request.Headers["environment"] to get value of "environment" header

这篇关于如何提取自定义标头值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆