我目前在我的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消息处理程序自定义头价值?
发布评论