Zha*_*ang 17 c# asp.net asp.net-mvc-4 asp.net-web-api web-api-contrib
我正在尝试记录我的Web API项目的HTTP响应标头.
该项目由VS2012,.NET 4.5和ASP.NET MVC 4开发.
我写了一个DelegatingHandler像这样的子类:
public class LoggingHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Execute the request
return base.SendAsync(request, cancellationToken).ContinueWith(task =>
{
var response = task.Result;
return response;
});
}
}
Run Code Online (Sandbox Code Playgroud)
但问题是,我无法从中获取标头值response.response.Headers是一个空集合,只response.Content.Headers包含一个名为的键Content-Type,而且HttpContext.Current是null.
我已经看到了WebAPIContrib的代码,它使用相同的逻辑来记录标题,但它们的代码似乎也不起作用.
那么我应该如何跟踪Web API项目中的HTTP响应标头?
你必须看对地方:
request.Content.Headers.ContentType
Run Code Online (Sandbox Code Playgroud)
假设有一个Content-Type: application/json请求标头,那么上述内容将返回"application/json".
所以基本上一些标题与内容相关联,这就是您应该从中阅读它们的地方。
响应标头也是如此。根据它们的类型,您可能需要从响应内容中提取它们(对于返回正文的请求)
response.Content.Headers.ContentType
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1243 次 |
| 最近记录: |