【问题标题】:How to retrieve the posted parameters at server side?如何在服务器端检索发布的参数?
【发布时间】:2013-07-22 14:22:50
【问题描述】:

我正在尝试在 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 一直是空的,我不知道如何检索发布的参数...

谁能帮帮我? 提前致谢!

【问题讨论】:

    标签: asp.net rest httpclient httprequest


    【解决方案1】:

    我发现这个有用的链接帮助了我: http://www.asp.net/web-api/tutorials/hands-on-labs/build-restful-apis-with-aspnet-web-api

    这行得通!

    【讨论】:

      猜你喜欢
      • 2014-09-03
      • 2017-03-30
      • 2012-03-11
      • 2022-07-07
      • 1970-01-01
      • 2016-02-27
      • 2015-04-14
      • 2015-10-19
      • 2018-08-09
      相关资源
      最近更新 更多