Asp.net mvc 如何使用`BindModel(HttpActionContext actionContext…`签名创建自定义模型绑定器?

Asp.net mvc 如何使用`BindModel(HttpActionContext actionContext…`签名创建自定义模型绑定器?,asp.net-mvc,asp.net-mvc-4,asp.net-web-api,imodelbinder,Asp.net Mvc,Asp.net Mvc 4,Asp.net Web Api,Imodelbinder,我需要知道如何在MVC4中创建自定义的IModelBinder,它已经被更改了 必须实施的新方法是: bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext); 有2个IModelBinder接口: 与以前的版本相同,并且没有更改 Web API和ApiController使用它。因此,基本上在这个方法中,您必须将actionContext.ActionArguments设置为相应的值

我需要知道如何在MVC4中创建自定义的
IModelBinder
,它已经被更改了

必须实施的新方法是:

bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext);

有2个IModelBinder接口:

  • 与以前的版本相同,并且没有更改
  • Web API和ApiController使用它。因此,基本上在这个方法中,您必须将
    actionContext.ActionArguments
    设置为相应的值。您不再返回模型实例
  • ,由Steve提供,提供了一个完整的答案。我将其添加到这里以供参考。asp.net论坛上的dravva将获得奖励

    首先,创建一个从
    IModelBinder
    派生的类。正如Darin所说,请确保使用
    System.Web.Http.ModelBinding
    命名空间,而不是熟悉的MVC等效名称空间

    public class CustomModelBinder : IModelBinder
    {
        public CustomModelBinder()
        {
            //Console.WriteLine("In CustomModelBinder ctr");
        }
    
        public bool BindModel(
            HttpActionContext actionContext, 
            ModelBindingContext bindingContext)
        {
            //Console.WriteLine("In BindModel");
            bindingContext.Model = new User() { Id = 2, Name = "foo" };
            return true;
        }
    }
    
    接下来,提供一个供应商,作为新活页夹的工厂,以及将来可能添加的任何其他活页夹

    public class CustomModelBinderProvider : ModelBinderProvider
    {
        CustomModelBinder cmb = new CustomModelBinder();
        public CustomModelBinderProvider()
        {
            //Console.WriteLine("In CustomModelBinderProvider ctr");
        }
    
        public override IModelBinder GetBinder(
            HttpActionContext actionContext,
            ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType == typeof(User))
            {
                return cmb;
            }
    
            return null;
        }
    }
    
    最后,在Global.asax.cs中包括以下内容(例如,应用程序启动)

    var配置=全局配置。配置;
    IEnumerable modelBinderProviderServices=configuration.ServiceResolver.GetServices(typeof(ModelBinderProvider));
    列表服务=新列表(modelBinderProviderServices);
    添加(新的CustomModelBinderProvider());
    configuration.servicesolver.SetServices(typeof(ModelBinderProvider),services.ToArray();
    
    现在,您可以将新类型作为参数添加到动作方法中

    public HttpResponseMessage<Contact> Get([ModelBinder(typeof(CustomModelBinderProvider))] User user)
    
    公共HttpResponseMessage获取([ModelBinder(typeof(CustomModelBinderProvider))]用户) 甚至

    public HttpResponseMessage<Contact> Get(User user)
    
    public-HttpResponseMessage-Get(用户)
    
    对托德帖子的RC更新:

    添加模型绑定器提供程序已简化:

    var configuration = GlobalConfiguration.Configuration;
    
    configuration.Services.Add(typeof(ModelBinderProvider), new YourModelBinderProvider());
    

    在不使用ModelBinderProvider的情况下添加modelbinder的一种简单方法是:

    GlobalConfiguration.Configuration.BindParameter(typeof(User), new CustomModelBinder());
    

    还必须注册自定义模型绑定器。ASP.Net Web API与MVC3的方式不同。请查看如何在MVC4 Beta版中进行注册。答案的底部很难说清楚,但请注意,您可以在
    global.asax.cs
    中使用
    GlobalConfiguration.Configuration.servicesolver.GetServices…
    对我来说是ked。有没有什么方法可以全局实现这一点,即设置默认的模型绑定器?我相信,只要您明确使用[ModelBinder(typeof(CustomModelBinderProvider))]在您的操作中,您不需要ModelBinderProvider。这非常有效!无论出于何种原因,我无法让本页上的任何其他示例与MVC4一起使用。ModelBinderProvider的界面似乎已更改。但是删除ModelBinderProvider并将此代码添加到应用程序中非常有效!
    GlobalConfiguration.Configuration.BindParameter(typeof(User), new CustomModelBinder());