如何添加和获得的WebAPI页眉值 [英] How to add and get Header values in WebApi
本文介绍了如何添加和获得的WebAPI页眉值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建的WebAPI POST方法,所以我可以从应用程序的WebAPI方法发送数据。我不是能得到头值
I need to create a POST method in WebApi so I can send data from application to WebApi method. I'm not able to get header value.
在这里,我已经在申请加入标头值:
Here I have added header values in the application:
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方法:
Following the WebApi post method:
public string Postsam([FromBody]object jsonData)
{
HttpRequestMessage re = new HttpRequestMessage();
var headers = re.Headers;
if (headers.Contains("Custom"))
{
string token = headers.GetValues("Custom").First();
}
}
什么是让头部值正确的方法?
What is the correct method for getting header values?
感谢。
推荐答案
在Web API的一面,简单地使用请求对象,而不是创造新的HttpRequestMessage
On the Web API side, simply use Request object instead of creating new HttpRequestMessage
var re = Request;
var headers = re.Headers;
if (headers.Contains("Custom"))
{
string token = headers.GetValues("Custom").First();
}
return null;
输出 -
Output -
这篇关于如何添加和获得的WebAPI页眉值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文