如何讓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