asp.net-mvc - 如何在 MVC4 中调用自定义模型绑定(bind)器?

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

因此,似乎有几个人(例如 herehere )对 ApiController 的 MVC4 模型绑定(bind)有问题,但他们似乎都没有完全解决我所看到的问题.

我真正想做的就是更改整数列表的数组绑定(bind)行为。假设我有一个这样的请求类型:

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>属性。

我已经尝试了常用的模型绑定(bind)技巧,但与 MVC4 中的大多数 Web API 一样,它似乎有一个完全独立的模型绑定(bind)路径。

我最远的是使用 System.Web.Http.ModelBinding.ModelBinder属性 MyRequestModel ,并创建一个“实现”的模型绑定(bind)器 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)

最佳答案

如果您正在谈论 ApiController,那么您正在尝试在 Web API 和现在的 MVC 中建模绑定(bind) 这是一个示例模型 Binder

  public class MyRequestModelBinderProvider : ModelBinderProvider
    {
        MyRequestModelBinder binder = new MyRequestModelBinder();
        public IdeaModelBinderProvider()
        {          
        }

        public override IModelBinder GetBinder(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType == typeof(MyRequestModel))
            {
                return binder;
            }

            return null;
        }
    } 

这是注册自定义模型绑定(bind)程序提供程序的示例

 IEnumerable<object> modelBinderProviderServices = GlobalConfiguration.Configuration.ServiceResolver.GetServices(typeof(ModelBinderProvider));
 List<Object> services = new List<object>(modelBinderProviderServices);
 services.Add(new MyRequestModelBinderProvider());
 GlobalConfiguration.Configuration.ServiceResolver.SetServices(typeof(ModelBinderProvider), services.ToArray());

现在,在自定义模型绑定(bind)器中,您可以使用上下文来访问查询字符串值

  public class MyRequestModelBinder :  IModelBinder
    {
        public MyRequestModelBinder()
        {

        }

        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            MyRequestModel yourModel; 
            //use contexts to access query string values
            //create / update your model properties

            bindingContext.Model = yourModel;  
            //return true || false if binding is successful
        }

确保您使用的是 WebAPI 而不是 MVC 的类和接口(interface)。有些名称相同,但命名空间和 dll 不同

关于asp.net-mvc - 如何在 MVC4 中调用自定义模型绑定(bind)器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10096957/

相关文章:

asp.net - mvc 在没有 httpContext 或 Controller 上下文的情况下渲染部分 View

jquery - 如何获取登录人员列表?

c# - Web Api $extend IQueryable 带过滤器

asp.net-mvc - 为什么 System.ComponentModel.DataAnnotations.DisplayAttribute 被密封?

asp.net-mvc - ELMAH MVC 2 - 温莎堡问题

asp.net - TextBoxFor 产生一个空白字段

c# - 防止 ASP.NET Web API 路由引擎扣除自定义方法名称

javascript - 从 javascript 链接到 .cshtml View

jQuery 不将表单输入传递给我的方法

c# - 从 App.OnStartup 调用异步 Web API 方法