我正在向WebApi方法发布对象。我使用PostAsJsonAsync
来做到这一点。PostAsJsonAsync后的WebApi方法中的对象null
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方法中为空。我似乎无法解决为什么我所遵循的所有例子都使用这种格式。
为什么调用对象不能被web 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; }
}
您能否粘贴异常消息。然而,看起来你的模型绑定不起作用 – Arsene
也在调试模式下运行它,并进入代码,你会发现更多关于你正在发送的数据 – Arsene
他没有得到一个异常消息,只是接收null,发生在我身上几次,它可能有不同的原因。既然你说方法的相同签名在其他情况下有效,我会问你是否发送和接收完全相同的sams类型,或者只是具有相同名称的类,但是在不同的命名空间中。如果第二个变体,请检查您是否将TypeNameHandling设置为auto或全部,我想在Global配置中,如果我还记得的话。 – meJustAndrew