【问题标题】:HTTPPOST request calling with string parameter使用字符串参数调用 HTTPPOST 请求
【发布时间】:2017-08-11 01:51:14
【问题描述】:

我有一个 POST 请求,它只有一个字符串参数

  [HttpPost]
    public IHttpActionResult CheckMfaEnabled([FromBody]string userEmail)//
     {
         var isMfaEnabled = //use another service to return true/false
         return Ok(isMfaEnabled);
     }

我这样称呼它

 using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:60099");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    StringContent content = new StringContent(JsonConvert.SerializeObject(userEmail), Encoding.UTF8, "application/json");
                var response = client.PostAsync("api/MFA/CheckMfaEnabled", content).Result;
                return response.IsSuccessStatusCode;
}

它让我 404 not found Error , 我想我调用 HTTPPOST 的方式有问题。 谁能帮帮我?

【问题讨论】:

    标签: json asp.net-web-api http-post httpclient webapi2


    【解决方案1】:

    这是一个路由问题。当我通过添加 [Route("api/MFA/CheckMfaEnabled")] 修改您的 API 方法以使用属性路由时,您的客户端代码可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      相关资源
      最近更新 更多