通过传递简单和复杂的参数调用 web api

call web api by passing simple and complex parameter

收到控制器中未找到 http 资源或操作的错误消息。在网络 api 中,我有一个 from body 和 from uri 参数。

[HttpPost]        
public IHttpActionResult processfields(
    [FromUri]string field1,
    [FromUri]string field2, 
    [FromBody] string field3, 
    [FromUri]string field4
){...}

在客户端中,我想调用网络 api,方法是--

using (var client = new HttpClient())
{
    //set up client
    client.BaseAddress = new Uri(Baseurl);
    client.DefaultRequestHeaders.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


     var values = new Dictionary<string, string>();
     values.Add("field1", field1);
     values.Add("field2", field2);
     values.Add("field3", field3);
     values.Add("field4", filed4);


     var jsonString = JsonConvert.SerializeObject(values);
     try
     {

         HttpResponseMessage Res = client.PostAsync("api/home/processfields", new StringContent(jsonString, Encoding.UTF8, "application/json")).Result;
         var result = Res.Content.ReadAsStringAsync();
     }
}

调试时,它执行上面的最后一行,但没有任何反应,错误消息显示--

{"Message":"No HTTP resource was found that matches the request URI 'http://xxx.xxx.xx.xx:1234/api/home/processfields'.","MessageDetail":"No action was found on the controller 'home' that matches the request."}

我的webapi.config有

// Web API routes
config.MapHttpAttributeRoutes();


config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

field1field2field4 参数应在 Url 中,而不是在请求正文中。这就是请求 Url 应如下所示的原因:http://xxx.xxx.xx.xx:1234/api/home/processfields?field1=value1&field2=value2&field4=value4

field3 从正文中反序列化。但是,由于它是字符串参数,请求主体不应构建为 JSON object:

{
  "field3": "value3"
}

但作为 JSON 字符串:

"value3"

这里是经过调整的客户端代码,应该可以工作:

using (var client = new HttpClient())
{
    //set up client
    client.BaseAddress = new Uri(Baseurl);
    client.DefaultRequestHeaders.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var requestUri = new Uri($"api/home/processfields?field1={HttpUtility.UrlEncode(field1)}&field2={HttpUtility.UrlEncode(field2)}&field4={HttpUtility.UrlEncode(field4)}", UriKind.Relative);
    var content = new StringContent($"\"{field3}\"", Encoding.UTF8, "application/json");
    var response = await client.PostAsync(requestUri, content);
    var result = await response.Content.ReadAsStringAsync();
}

首先,查看 Web API 方法签名,我认为这里根本不涉及任何 JSON。所以我认为你可以跳过 Accept header 的设置。 (如果 [FromBody] 参数是一个复杂的 object 而不是字符串,那么 JSON 序列化可能会发挥作用。)

其次,永远不要将 .Result 与 HttpClient 一起使用。这是一个 async-only API,你应该使用 async / await,否则你只是在邀请死锁。

最后,正如其他人所指出的,field1、field2 和 field4 进入查询字符串,而不是 body。如果你想要一种更有条理的方式来做到这一点,并且总体上减少仪式,请考虑使用 Flurl(免责声明:我是作者),它在后台使用 HttpClient:

using Flurl.Http;

var result = await BaseUrl
    .AppendPathSegment("api/home/processfields")
    .SetQueryParams(new { field1, field2, field4 })
    .PostStringAsync(field3)
    .ReceiveString();
bodyClass obj = new bodyClass();
obj.empName= "your param value";
string DATA = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

var client = new HttpClient();
HttpContent content = new StringContent(DATA, UTF8Encoding.UTF8, "application/json");
HttpResponseMessage messge = client.PostAsync("your api url", content).Result;

if (messge.IsSuccessStatusCode)
{
      string result = messge.Content.ReadAsStringAsync().Result;
}