ASP.NET Web API中对Headers的操作
原文:https://blog.csdn.net/pan_junbiao/article/details/84069105
ASP.NET Web API中对Headers的操作。
1、获取请求头信息
string value = HttpContext.Current.Request.Headers["name"];
2、添加响应头信息
HttpResponseMessage result = new HttpResponseMessage();
result.Headers.Add("name", "value");
示例:ASP.NET Web API项目方法中获取请求头信息,并在返回结果中添加响应头信息。
/// <summary>
/// Headers操作示例
/// </summary>
public HttpResponseMessage OperHeaders()
{
//获取请求头信息
string info = HttpContext.Current.Request.Headers["My-Headers-Info"];
//返回响应结果
HttpResponseMessage result = new HttpResponseMessage();
result.Content = new StringContent("请求头信息为:" + info);
//添加响应头信息
result.Headers.Add("Access-Control-Expose-Headers", "My-Headers-Info");
result.Headers.Add("My-Headers-Info", "ABC123");
return result;
}