PostAsJsonAsync之后的WebApi方法中的对象为null

时间:2016-06-14 17:20:20

标签: c# asp.net-mvc asp.net-web-api http-post asp.net-web-api2

我正在向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不为空。

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

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

为什么呼叫对象不被网络api选中?

修改

这是ServiceCall对象。它位于单独的类库中,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; }
}

4 个答案:

答案 0 :(得分:3)

使用前缀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()
{

}

答案 1 :(得分:2)

由于序列化,我在 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)
            {

            }
        }

答案 2 :(得分:1)

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

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", call);
    return response;
}

答案 3 :(得分:0)

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

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