そのため、何人かの人々 (ここやここなど) が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>
私は通常のモデル バインディングのトリックを試しましたが、MVC4 のほとんどの Web API と同様に、モデル バインディングにはまったく別のパスがあるようです。
私が得た最も遠いものは、 のSystem.Web.Http.ModelBinding.ModelBinder
属性を使用しMyRequestModel
、 を「実装」したモデル バインダーを作成することSystem.Web.Http.ModelBinding.IModelBinder
です。これにより、コードに触れることのないスタック トレースで常にオブジェクト参照例外が発生します。
これ打った人いる?次に何をしようか考えていますか?
更新: カスタムでキャプチャしたスタック トレースは次の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)