我需要在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();
}
}
获取标头值的正确方法是什么?
谢谢。
string token = headers.GetValues("Custom").FirstOrDefault();
吗?编辑:刚注意到您正在匹配原始Qs样式。