【发布时间】:2012-04-12 05:06:12
【问题描述】:
我需要知道如何在 MVC 4 中创建自定义 IModelBinder,它已被更改。
必须实现的新方法是:
bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext);
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-4 asp.net-web-api imodelbinder
我需要知道如何在 MVC 4 中创建自定义 IModelBinder,它已被更改。
必须实现的新方法是:
bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext);
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-4 asp.net-web-api imodelbinder
IModelBinder 接口有 2 个:
System.Web.Mvc.IModelBinder 和之前的版本一样,没有变化System.Web.Http.ModelBinding.IModelBinder 由 Web API 和 ApiController 使用。所以基本上在这个方法中,你必须将actionContext.ActionArguments 设置为相应的值。您不再返回模型实例。【讨论】:
global.asax.cs 和 GlobalConfiguration.Configuration.ServiceResolver.GetServices...
This link,由 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 中包含以下内容(例如 Application_Start)。
var configuration = GlobalConfiguration.Configuration;
IEnumerable<object> modelBinderProviderServices = configuration.ServiceResolver.GetServices(typeof(ModelBinderProvider));
List<Object> services = new List<object>(modelBinderProviderServices);
services.Add(new CustomModelBinderProvider());
configuration.ServiceResolver.SetServices(typeof(ModelBinderProvider), services.ToArray());
现在,您可以将新类型作为参数传递给您的操作方法。
public HttpResponseMessage<Contact> Get([ModelBinder(typeof(CustomModelBinderProvider))] User user)
甚至
public HttpResponseMessage<Contact> Get(User user)
【讨论】:
Todd 帖子的 RC 更新:
已简化添加模型绑定器提供程序:
var configuration = GlobalConfiguration.Configuration;
configuration.Services.Add(typeof(ModelBinderProvider), new YourModelBinderProvider());
【讨论】:
在没有 ModelBinderProvider 的情况下添加模型绑定器的一种更简单的方法是:
GlobalConfiguration.Configuration.BindParameter(typeof(User), new CustomModelBinder());
【讨论】: