【发布时间】:2014-02-19 16:49:11
【问题描述】:
我需要在 WebApi 中创建一个 POST 方法,以便可以将数据从应用程序发送到 WebApi 方法。我无法获取标头值。
这里我在应用程序中添加了标头值:
using (var client = new WebClient())
{
// Set the header so it knows we are sending JSON.
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers.Add("Custom", "sample");
// Make the request
var response = client.UploadString(url, jsonObj);
}
遵循WebApi post方法:
public string Postsam([FromBody]object jsonData)
{
HttpRequestMessage re = new HttpRequestMessage();
var headers = re.Headers;
if (headers.Contains("Custom"))
{
string token = headers.GetValues("Custom").First();
}
}
获取标头值的正确方法是什么?
谢谢。
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-4 c#-4.0 asp.net-web-api