反序列化/模型MVC4的WebAPI绑定不使用数组 [英] Deserialization / model binding in MVC4 webapi does not work with arrays

查看:147
本文介绍了反序列化/模型MVC4的WebAPI绑定不使用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是新的WebAPI这是MVC4测试的一部分。我有以下类:

I'm using the new WebApi which is part of MVC4 beta. I have the following class:

public class Voucher
{
    public string Id { get; set; }
    public string TableId { get; set; }
    public Product[] Products { get; set; } 
}

我的控制器看起来是这样的:

My controller looks like this:

public class VouchersController : ApiController
{
    public Voucher PostVoucher(Voucher voucher)
    {
          //....
    }
}

在客户端使用的XmlSerializer 我序列数据。输出看起来像预期和产品数组是序列化。如果我发布的数据,并把一个破发点的 PostVoucher 方法中,我得到了编号和<$数据C $ C> TABLEID ,但产品。任何暗示什么,我可能是做错了?

On the client side I serialize the data using an XmlSerializer. The output looks like expected and the Products array is serialized. If I post the data and put a break point inside the PostVoucher method, I get the data for Id and TableId, but Products is null. Any hint what I might be doing wrong?

推荐答案

有与模型中的ASP.NET Web API测试版绑定复杂类型和XML /反序列化的问题。解决这个问题的方法之一是为禁用模式的结合,并选择这个动作的反序列化路径。该职位在<一个href=\"http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/27/disabling-model-binding-on-asp-net-web-apis-beta.aspx\">http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/27/disabling-model-binding-on-asp-net-web-apis-beta.aspx展示了如何做到这一点。

There is an issue with the model binding / deserialization of complex types and XML in the ASP.NET Web API Beta. One way to solve this issue is to "disable" model binding, and select the "deserialization path" for this action. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/27/disabling-model-binding-on-asp-net-web-apis-beta.aspx shows how to do that.

请注意,这是一个测试版已知的问题,并将在下一(RC)版本。

Notice that this is a known issue for beta, and will be fixed in the next (RC) release.

这篇关于反序列化/模型MVC4的WebAPI绑定不使用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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