HttpClient将多个对象/变量传递给[HttpPost] Web API m

时间:2016-08-02 07:40:39

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

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

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

客户控制器:

sed

API方法:

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);


} 

我收到错误:

[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;
}

1 个答案:

答案 0 :(得分:0)

请在提出请求前仔细检查您是否有正确的404package com.controller; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.json.JSONArray; import org.json.JSONObject; public class JSONColumnNameExtract { static List<String> colNames; public static void main(String[] args) { colNames = new ArrayList<String>(); String jsonString = ""; try { jsonString = readFile("C:\\jsonInput.json", StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } JSONObject mainJObject = new JSONObject(jsonString); Iterator<?> keys = mainJObject.keys(); while (keys.hasNext()) { String key = (String) keys.next(); if (mainJObject.get(key) instanceof JSONArray) { JSONArray array = (JSONArray) mainJObject.get(key); for (int i = 0; i < array.length(); i++) { iterateJSON(array.get(i), key); } continue; } if (mainJObject.get(key) instanceof JSONObject) { iterateJSON(mainJObject.get(key), key); } else { if (!colNames.contains(key)) colNames.add(key); } } for (String colName : colNames) System.out.println(colName); } private static void iterateJSON(Object object, String key2) { JSONObject jsonObject = ((JSONObject) object); Iterator<?> keys = jsonObject.keys(); String key; while (keys.hasNext()) { key = (String) keys.next(); if (jsonObject.get(key) instanceof JSONArray) { JSONArray array = (JSONArray) jsonObject.get(key); for (int i = 0; i < array.length(); i++) { iterateJSON(array.get(i), key); } continue; } if (jsonObject.get(key) instanceof JSONObject) { iterateJSON(jsonObject.get(key), key2 + "." + key); } else { if (!colNames.contains(key2 + "." + key)) colNames.add(key2 + "." + key); continue; } } } static String readFile(String path, Charset encoding) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); return new String(encoded, encoding); } } 表示资源 未找到。

相关问题