【发布时间】:2017-04-12 02:52:34
【问题描述】:
如何让 asp.net webapi 查看路由数据和主体以绑定到复杂对象?
使用以下路由“api/products/{productid}/manufacturers/{manufacturerId}”我需要productId和manufacturerId绑定到模型,所以我的控制器方法是
public IHttpActionResult Create(CreateProductManufacturerModel
createProductManufacturerModel)
我的模型是
public class CreateProductManufacturerModel
{
public int ProductId { get; set; }
public int ManufacturerId { get; set; }
public bool IsDefault { get; set; }
public string ManufacturerCode { get; set; }
public string Description { get; set; }
}
我知道我可以将我的方法更改为如下,但我正在使用 fluentvalidation 来验证整个 createproductmanufacturermodel,这是自动完成的(请参阅-http://www.justinsaraceno.com/2014/07/fluentvalidation-with-webapi2/)。因此productId 和manufacturerId 将无法正确验证,因为它们设置为零。
public IHttpActionResult Create(int productId, int manufacturerId, CreateProductManufacturerModel
createProductManufacturerModel)
我已经尝试过 modelbinder,但它不会自动触发 fluentvalidation。不太确定这是否重要,但发布的正文是 json 格式。
提前致谢。
保罗
【问题讨论】:
-
为什么不能在
createproductmanufacturermodel中添加productId和manufacturerId以及json数据中的其他值? -
你可以,但这让我觉得很脏,因为数据提供了两次
标签: c# asp.net-web-api