相关疑难解决方法(0)

如何在MVC4中调用自定义模型绑定器?

所以似乎有几个人(比如这里这里)在ApiControllers上遇到了MVC4模型绑定的问题,但是他们似乎都没有解决我看到的问题.

我真正想做的就是更改整数列表的数组绑定行为.所以说我有这样的请求类型:

public class MyRequestModel
{
    public List<long> ListOfIntegers { get; set; }

    ...
}
Run Code Online (Sandbox Code Playgroud)

和这样的API GET方法:

public ResultsResponseModel Get(MyRequestModel request)
{
    // use request.ListOfIntegers meaningfully

    ...

    return response;
}
Run Code Online (Sandbox Code Playgroud)

我基本上希望能够说出/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() …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-mvc-4 asp.net-web-api

5
推荐指数
1
解决办法
5297
查看次数