如何让asp.net webapi查看路径数据和正文以绑定到复杂对象?c#Webapi和模型与fluentvalidation的绑定
使用以下路线“API /产品/ {产品ID} /厂商/ {manufacturerId}”我需要产品ID和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)
我已经试过模型绑定器,但它不会自动触发fluentvalidation。不太确定这是否重要,但发布的主体是json格式。
在此先感谢。
保罗
为什么不能在'createproductmanufacturermodel'中添加productId和manufacturerId以及json数据中的其他值? – user3014311
你可以,但这让我感觉很脏,因为数据提供了两次 – phooey