所以好像有幾個人(如here和here)與MVC4模型綁定ApiControllers有問題,但他們都沒有解決我看到的問題。如何在MVC4中調用自定義模型聯編程序?
我真的很想做的就是改變整數列表的數組綁定行爲。所以說,我有一個請求類型是這樣的:
public class MyRequestModel
{
public List<long> ListOfIntegers { get; set; }
...
}
而且像這樣的API GET方法:
public ResultsResponseModel Get(MyRequestModel request)
{
// use request.ListOfIntegers meaningfully
...
return response;
}
我基本上要能夠說/api/results/?listOfIntegers=1+2+3+4+5
和有決心的List<long>
財產。
我已經嘗試了我通常的模型綁定技巧,但是與大多數Web API在MVC4中似乎有一個完全獨立的模型綁定路徑。
我已經使用上MyRequestModel
一個System.Web.Http.ModelBinding.ModelBinder
屬性,並創建一個模型綁定該「實施」 System.Web.Http.ModelBinding.IModelBinder
得到最遠。這始終會產生一個對象引用異常,它不會觸及我的代碼。
有人打過嗎?想想接下來要嘗試什麼?
UPDATE:下面是我在我的自定義ExceptionFilterAttribute
已經捕捉到了一個堆棧跟蹤:
Object reference not set to an instance of an object.
at System.Web.Http.ModelBinding.DefaultActionValueBinder.BindParameterValue(HttpActionContext actionContext, HttpParameterBinding parameterBinding)
at System.Web.Http.ModelBinding.DefaultActionValueBinder.<>c__DisplayClass1.BindValuesAsync>b__0(RequestContentReadKind contentReadKind)
at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass38.<ToAsyncVoidTask>b__37()
at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
您可以提交堆棧跟蹤嗎? – jorgehmv 2012-04-10 22:03:00
剛剛添加堆棧跟蹤 – 2012-04-10 22:37:38