PostAsJsonAsync 后 WebApi 方法中的对象 null

Object null in WebApi method after PostAsJsonAsync

我正在 post 将对象连接到 WebApi 方法。我正在使用 PostAsJsonAsync 来执行此操作。

public async Task<HttpResponseMessage> PostAsync(string token, ServiceCall call)
{
    var client = new HttpClient();
    client.SetBearerToken(token);

    var response = await client.PostAsJsonAsync(Uri + "id/nestedcall", call);

    return response;
}

我传递的对象 call 在我 post 时不为空。

[HttpPost]
[Route("id/nestedcall")]
public async Task<IHttpActionResult> NestedCall([FromBody]ServiceCall call)
{
    // call is null here
}

但是在我的 API 方法中它是空的。我似乎无法弄清楚为什么我遵循的所有示例都使用这种格式。

为什么调用对象没有被网络拾取 api?

编辑

这是 ServiceCall 对象。它位于单独的 class 库中,Web 应用程序和 API.

中均包含参考
public class ServiceCall
{
    public ServiceCall(Service service, string grantType)
    {
        ClientId = service.Id;
        ClientSecret = service.Secret;
        Uri = service.Uri;
        Scope = service.Scope;
        GrantType = grantType;
    }

    public ServiceCall(string clientid, string clientsecret, string uri, string scope, string grantType)
    {
        ClientId = clientid;
        ClientSecret = clientsecret;
        Uri = uri;
        Scope = scope;
        GrantType = grantType;
    }

    public string ClientId { get; set; }
    public string ClientSecret { get; set; }
    public string Uri { get; set; }
    public string Scope { get; set; }
    public string GrantType { get; set; }
}

看起来 JSON 序列化可能失败了。顺便说一句,删除 [FromBody] 并尝试不使用它,如下所示。 PostAsJsonAsync 方法将 ServiceCall 对象序列化为 JSON,然后在 POST 请求中发送 JSON 有效负载。

public async Task<IHttpActionResult> NestedCall(ServiceCall call)
{
    // your code
}

使用 Prefix Stackify 我能够诊断出序列化程序抛出异常:

Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type Core.Models.ServiceCall. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'ClientId', line 1, position 12.
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize

但是,非常有帮助,控制器没有告诉我发生了异常,而是给了我一个空对象。

正如异常所暗示的那样,解决方案是添加一个默认构造函数(或至少一个序列化程序可以理解的构造函数)。

public ServiceCall()
{

}

我 运行 遇到了完全相同的问题,不得不这样做来解决它:

using (var client = new HttpClient())
{
    client.SetBearerToken(token);
    var content = new StringContent(JsonConvert.SerializeObject(call), Encoding.UTF8, "application/json");

    var response = await client.PostAsJsonAsync(Uri + "id/nestedcall", content);
    return response;
}

由于序列化,我在 PostAsJsonAsync 之后在 WebApi 方法中看到了 Object null。 最好像下面这样使用 PostAsync :

        var obj = new MyClass()
        {
            MyProperty = 11
        };

        using (var client = new HttpClient())
        {
            string inputJson = Newtonsoft.Json.JsonConvert.SerializeObject(obj); 
            HttpContent inputContent = new StringContent(inputJson, Encoding.UTF8, "application/json");
            HttpResponseMessage response1 = client.PostAsync("http://localhost:60909/api/home/Test", inputContent).Result;
            if (response1.IsSuccessStatusCode)
            {

            }
        }