【问题标题】:HttpClient Pass Multiple objects/variables to[HttpPost] Web API mHttpClient 将多个对象/变量传递给[HttpPost] Web API m
【发布时间】:2016-08-02 07:40:38
【问题描述】:

我正在使用 HttpClient 将 .NET 项目与我的 Web API 连接起来。

我想知道如何在 PostAsJsonAsync() 方法中向 webapi 方法发送多个参数。

客户端控制器:

callbackUrl = Url.HttpRouteUrl("DefaultApiAction", new { controller = "Employee", action = "UpdateToleranceRangeByEmployeeId" });
responseMessage = await client.PostAsJsonAsync(callbackUrl, new { companyId = CompanyId, deptId = Depts, divId = Divs, empId = Emps, range = objToleranceRangeModel.ToleranceRange.Value, flag = objToleranceRangeModel.ToleranceFlag });
if (responseMessage.IsSuccessStatusCode)
{
    var responseData = responseMessage.Content.ReadAsStringAsync().Result;

}
else
{
    logger.Error("Error Inside UpdateToleranceRangeByEmployeeId:" + responseMessage.IsSuccessStatusCode);


} 

API 方法:

[AllowAnonymous]
[HttpPost]
[Route("UpdateToleranceRangeByEmployeeId")]
public HttpResponseMessage UpdateToleranceRangeByEmployeeId(int companyId, List<int> deptId, List<int> divId, List<int> empId, double range, bool flag)
{
    logger.Info("Inside UpdateToleranceRangeByEmployeeId");
    HttpResponseMessage response;
    List<EmployeeModel> ObjEmployeeModelList = new List<EmployeeModel>();

    try
    {


        ObjEmployeeModelList = this._iEmployeeServices.UpdateToleranceRangeByEmployeeId(companyId, deptId, divId, empId, range, flag);
        if (ObjEmployeeModelList != null && ObjEmployeeModelList.Count() != 0)
        {
            response = Request.CreateResponse<List<EmployeeModel>>(HttpStatusCode.OK, ObjEmployeeModelList);
        }
        else
        {
            response = Request.CreateResponse(HttpStatusCode.NotFound);
        }
    }
    catch (Exception objEx)
    {
        logger.Error("Error Inside UpdateToleranceRangeByEmployeeId:" + objEx);
        response = Request.CreateResponse<Exception>(HttpStatusCode.ExpectationFailed, objEx);
    }
    return response;
}

我收到错误:

responseMessage = {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Pragma: no-cache
  X-SourceFiles: =?UTF-8?B?RjpcU0FUXFNBVC5BUElcYXBpXEVtcGxveWVlXFVwZGF0ZVRvbGVyYW5jZVJhbmdlQnlFbXBsb3llZUlk?=
  Cache-Contro...

【问题讨论】:

  • 在参数上使用[FromBody] 属性将帖子正文的属性绑定到您的参数。

标签: c# asp.net-web-api2 httpclient


【解决方案1】:

在发出请求之前,请仔细检查您的URL 是否正确。 404 表示找不到资源。

【讨论】:

  • 是的,我已经检查过它的正确性......我认为我发送的参数会产生问题
猜你喜欢
  • 2016-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
相关资源
最近更新 更多