我们在控制器中有我们的主api有两个参数,但问题是我们将这些参数发送到api路径,有一个错误说“” 没有动作或方法匹配“。所以我们很困惑如何成功调用我们的主api,它返回响应对象中的数据。
RestService.BaseUrl = baseUrl;
var client = RestService.Instance;
var request = RestService.GetRestRequest("Payment/SyncWithIrd",
Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddHeader("application/json; charset=utf-8",
JsonConvert.SerializeObject(bmodel), ParameterType.RequestBody);
request.AddHeader("application/json; charset=utf-8",
JsonConvert.SerializeObject(mode)`enter code here`,
parameterType.RequestBody);
request.AddHeader("Content-Type", "application/json;charset=utf-8");
var response = client.Execute(request);
var value = response.Content;
return value;
答案 0 :(得分:0)
我在你的代码中看到了一些AddHeader但没有一个AddBody。 usualy verb Post有身体部位。尝试在方法签名中添加[FromBody],如
[HttpPost]
public HttpResponseMessage SyncWithIrd([FromBody]BillViewModel model, [FromBody]string mode)
然后删除
request.AddHeader("application/json; charset=utf-8", JsonConvert.SerializeObject(bmodel), ParameterType.RequestBody);
request.AddHeader("application/json; charset=utf-8", JsonConvert.SerializeObject(mode)`enter code here`, parameterType.RequestBody);
然后添加AddHeader
request.AddBody(new { model = JsonConvert.SerializeObject(bmodel) }, new { mode = JsonConvert.SerializeObject(mode) });
PS。在调用动词后总是使用身体部位。
编辑..
将SyncWithIrd的参数“string mode”放在BillViewModel中,并重建您的方法签名,如:
[HttpPost]
public HttpResponseMessage SyncWithIrd([FromBody]BillViewModel model)
然后AddBody必须
request.AddBody(new { model = bmodel } });
PS。我使用类似的代码:
var restClient = new RestClient(model.ApiUrl);
var restRequest = new RestRequest("api/auth/createuser", Method.POST);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddBody(new {
FullName = model.FullName,
Email = model.Email,
Username = model.UserName,
Password = model.Password
});
var restResponse = restClient.Execute(restRequest);
我希望它有所帮助。 LMK