如何提取网页API消息处理程序自定义头价值?

编程入门 行业动态 更新时间:2024-10-23 03:17:57
本文介绍了如何提取网页API消息处理程序自定义头价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前在我的Web API服务的消息处理程序,覆盖'SendAsync'如下:

I currently have a message handler in my Web API service that overrides 'SendAsync' as follows:

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { //implementation }

在这个code我需要检查命名的自定义添加请求头值 MyCustomID 。问题是,当我做到以下几点:

Within this code I need to inspect a custom added request header value named MyCustomID. The problem is when I do the following:

if (request.Headers.Contains("MyCustomID")) //OK var id = request.Headers["MyCustomID"]; //build error - not OK

...我收到以下错误消息:

...I get the following error message:

无法适用与[]索引到类型的前pression  System.Net.Http.Headers.Htt prequestHeaders

Cannot apply indexing with [] to an expression of type 'System.Net.Http.Headers.HttpRequestHeaders'

我怎样才能访问的单的通过自定义请求头中的Htt prequestMessage ( MSDN文档)实例传入此重写的方法?

How can I access a single custom request header via the HttpRequestMessage (MSDN Documentation) instance passed into this overridden method?

推荐答案

尝试是这样的:

IEnumerable<string> headerValues = request.Headers.GetValues("MyCustomID"); var id = headerValues.FirstOrDefault();

还有上一个头的方法TryGetValues​​你可以使用,如果你并不总是保证有机会获得头。

There's also a TryGetValues method on Headers you can use if you're not always guaranteed to have access to the header.

更多推荐

如何提取网页API消息处理程序自定义头价值?

本文发布于:2023-10-13 00:06:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1486255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   消息   价值   网页   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!