简体   繁体   English

为什么我不能为列表注册自定义模型活页夹 <int> ?

[英]Why can't I register a custom model binder for a List<int>?

I have an action that looks like 我有一个看起来像的动作

public ActionResult GetUsers(List<int> userIds) {//do stuff}

The list of userIds can become quite long, so I want to use Json.Net to deserialize it. userId的列表可能会变得很长,因此我想使用Json.Net对其进行反序列化。 To do so I created an IModelBinder implementation, which works fine for other objects, but never gets called for a List. 为此,我创建了一个IModelBinder实现,该实现对于其他对象也可以正常工作,但是永远不会被List调用。 The IModelBind looks like this IModelBind看起来像这样

public class JsonBinder : System.Web.Mvc.IModelBinder
{
  public object BindModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
  { //Do model binding stuff using Json.Net }
} 

And I register this model binder with this line 我在这行注册这个模型活页夹

ModelBinders.Binders.Add(typeof(List<int>), new JsonBinder());

However the JsonBinder is never called. 但是,永远不会调用JsonBinder。 Why is this? 为什么是这样? Should I be using a ValueProvider? 我应该使用ValueProvider吗?

Add the following event in global.asax (or add the code to your existing Application_BeginRequest handler): 在global.asax中添加以下事件(或将代码添加到现有的Application_BeginRequest处理程序中):

protected void Application_BeginRequest()
{
    foreach (var type in ModelBinders.Binders.Keys)
    {
        System.Diagnostics.Trace.WriteLine(
                              String.Format("Binder for '{0}': '{1}'", 
                                             type.ToString(), 
                                             ModelBinders.Binders[type].ToString()));
    }

}

Then you can check in the VS output window what binders are currently registered. 然后,您可以在VS输出窗口中检查当前已注册哪些活页夹。 You could see something like this: 您可能会看到以下内容:

Binder for 'System.Web.HttpPostedFileBase': 'System.Web.Mvc.HttpPostedFileBaseModelBinder'
Binder for 'System.Byte[]': 'System.Web.Mvc.ByteArrayModelBinder'
Binder for 'System.Data.Linq.Binary': 'System.Web.Mvc.LinqBinaryModelBinder'
Binder for 'System.Threading.CancellationToken': 'System.Web.Mvc.CancellationTokenModelBinder'

You could also check if there is any ModelBinderProvider that could be selecting the binder provider, because the order for selecting which model binder is used is as follows: 您还可以检查是否有任何ModelBinderProvider可以选择活页夹提供程序,因为选择使用哪种模型活页夹的顺序如下:

  1. The attribute on the parameter of the action. 操作参数上的属性。 See GetParameterValue method of the ControllerActionInvoker class 请参见ControllerActionInvoker类的 GetParameterValue方法

  2. The Binder returned from the IModelBinderProvider. 活页夹从IModelBinderProvider返回。 See GetBinder method in the ModelBinderDictionary class 请参见ModelBinderDictionary类中的GetBinder方法

  3. The Binder globally registered in the ModelBinders.Binders dictionary. 活页夹已在ModelBinders.Binders词典中全局注册。

  4. The Binder defined in the [ModelBinder()] attribute for the model type. [ModelBinder()]属性中为模型类型定义的Binder。

  5. The DefaultModelBinder. DefaultModelBinder。

Use a similar approach to check the model binder providers in the BeginRequest event: 使用类似的方法在BeginRequest事件中检查模型绑定程序提供程序:

foreach (var binderprovider in ModelBinderProviders.BinderProviders)
{
    System.Diagnostics.Trace.WriteLine(String.Format("Binder for '{0}'", binderprovider.ToString()));
}

Additionally, you could try to add Glimpse via nuget, as one of the tabs provides information about the model binder used for each of the parameters in the controller action. 此外,您可以尝试通过nuget添加Glimpse ,因为其中一个选项卡提供了有关用于控制器操作中每个参数的模型绑定程序的信息。

Hopefully this will help you tracing why your model binder is not being used. 希望这将帮助您找到未使用模型联编程序的原因。

Have you tried using ModelBinder attribute in the action method? 您是否尝试过在action方法中使用ModelBinder属性?

public ActionResult GetUsers([ModelBinder(typeof(JsonBinder))] List<int> userIds)

Ref: https://msdn.microsoft.com/en-us/library/system.web.mvc.modelbinderattribute(v=vs.118).aspx 参考: https : //msdn.microsoft.com/zh-cn/library/system.web.mvc.modelbinderattribute(v= vs.118) .aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM