PostAsJsonAsync之后,WebApi方法中的对象为null [英] Object null in WebApi method after PostAsJsonAsync

查看:69
本文介绍了PostAsJsonAsync之后,WebApi方法中的对象为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将对象发布到WebApi方法.我正在使用 PostAsJsonAsync 来做到这一点.

I am posting an object to a WebApi method. I'm using PostAsJsonAsync to do this.

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 在发布时不为空.

The object call that I'm passing is not null when I post it.

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

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

However it is null in my API method. I can't seem to work out why as all of the examples I've followed use this format.

为什么网络api无法拾取呼叫对象?

Why isn't the call object being picked up by the web api?

修改

这里是 ServiceCall 对象.它在单独的类库中,并且Web应用程序和API中都包含引用.

Here is the ServiceCall object. It is in a separate class library and a reference is included in both the web application and the 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; }
}

推荐答案

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

Using Prefix Stackify I was able to diagnose that the serialiser was throwing an exception:

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

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

However, very helpfully, rather than tell me that an exception occurred the controller simply gave me a null object.

该异常提示该解决方案是添加一个默认构造函数(或至少一个序列化程序可以理解的构造函数).

As hinted by the exception the solution is to add a default constructor (or at least one the serialiser can understand).

public ServiceCall()
{

}

这篇关于PostAsJsonAsync之后,WebApi方法中的对象为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆