我正在开发一个 WebAPI 服务并且遇到了一个奇怪的问题。使用构造函数调用 ViewModel 时,为什么返回类型从 XML 更改为 JSON?
来自控制器的代码;
// GET api/Product/5
public MyViewModel GetProduct(Int64 id)
{
    // without a constructor this returns an xml
    //return new MyViewModel() { Name = "123" };
    // this changes type to json
    Product product = new Product();
    return new MyViewModel(product) { Name = "123" };
}
查看模型类;
[XmlRoot(ElementName = "ARequest", Namespace = "http://myschema.com/schemas/myviewmodel.xsd")]
public class MyViewModel : Product
{
    public string Name { get; set; }
    public MyViewModel(Product product)
    {
        // this constructor causes the type to switch from
        // xml to json - why?
    }
}