ASP.NET Web API中對Headers的操作
阿新 • • 發佈:2018-11-26
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; }