我正在尝试在 ASP.Net 上实现一个 RESTful Web API。为了测试这个 Web API,我创建了一个使用 HttpClient.PostAsync 的小型客户端应用程序。我在 HttpContent 对象中添加了一些参数,但无论我尝试什么,我都无法在我的 Web API 的服务器端找到这些发布的参数。
客户端代码:
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var customer = new Customer() { FirstName = "test", LastName = "test" };
MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
HttpContent content = new ObjectContent<Customer>(customer, jsonFormatter);
HttpResponseMessage response = await client.PostAsync(base_url, content);
服务器端代码:
string httpMethod = Request.HttpMethod;
if (httpMethod == "POST")
{
string firstName = Request.QueryString["FirstName"];
string lastName = Request.QueryString["LastName"];
}
如果我在服务器端设置断点,我会看到 Request.AcceptTypes 等于“application/json”,因此可能是在服务器端收到了格式类型。但是, Request.QueryString 一直是空的,我不知道如何检索发布的参数......
谁能帮我吗?提前致谢!