使用ModelBinder的属性与ModelBinders.Add() [英] Using ModelBinder attribute vs. ModelBinders.Add()

查看:520
本文介绍了使用ModelBinder的属性与ModelBinders.Add()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我的利弊/ CONCS使用 [ModelBinder的()] 属性与通过注册模型粘合剂ModelBinders.Add()在Global.asax中?

Can someone tell me the pros/concs to using [ModelBinder()] attribute vs. registering model binders via ModelBinders.Add() in global.asax?

一个优点,我能想到的是,它是更加明确,而在注册全球 ModelBinders 不如abvious某人检查操作方法。

One advantage I can think of is that it's more explicit, whereas registering in the global ModelBinders is not as abvious to someone inspecting the action method.

一个权衡,我能想到的是,它是不能重用的,因为你必须把这个属性添加到需要使用这种模型绑定所有的操作方法,而在全球注册 ModelBinders 将使其可用于接收该模型的所有的操作方法。

One tradeoff I can think of is that it's not reusable, since you would have to add this attribute to all action methods that need to use this model binder, whereas registering in the global ModelBinders will make it available for all action methods receiving that model.

这是唯一的区别?

在换句话说,就说明这是正确的:

In other words, would stating this be correct:


  • 如果你只在一个动作方法(也许两年,拿到+安置)使用该模型,然后使用 [ModelBinder的()]

  • 如果您使用一个以上的动作方法的模型,然后在全球 ModelBinders
  • 注册它
  • If you only use the model in one action method (maybe two, get + post), then use [ModelBinder()].
  • If you use the model in more than one action method, then register it in the global ModelBinders.

推荐答案

的那些技术,结果将是相同的,因此主要是什么样的球队感觉更舒服的问题。所以,你可以拿出像你说的一个惯例。

The result of those techniques will be the same so it is mostly a matter of what the team feels more comfortable with. So you could come up with a convention like the one you stated.

我个人$不必设置使用该模型中的每个操作方法属性p $ PFER。所以我会选择下列选项之一:

Personally, I prefer not having to set the attribute on every action method that uses that model. So I would choose one of the following options:


  • 设置属性的模型类,如:

  • Set the attribute on the model class like:

[ModelBinder(typeof(MyModelBinder))]
public class MyModel
{
    ...
}    


  • 在全球范围内注册的文件夹

  • Globally register the binder

    ModelBinders.Binders.Add(typeof(MyModel), new MyModelBinder())
    


  • 另一个原因,我preFER其中之一是因为如果你曾经有手动触发模型绑定过程中,你可能还需要使用您的自定义模型绑定:

    Another reason why I prefer one of those is because if you ever have to manually trigger the model binding process, you may also want your custom model binder to be used:

    public ActionResult SomeActionMethod()
    {
         MyModel model = ...
    
         //manually invoke the model binding process considering only query string data
         //The custom model binder will be used only if it was globally registered
         //in the binders dictionary or set in an attribute of the model class
         TryUpdateModel(model, new QueryStringValueProvider())
    
         ...
    }
    

    您也可以在

    ModelBinderProviders.BinderProviders.Add(new CustomModelBinderProvider()) 
    

    在该方法中的参数用的属性的一个方法,可以重写该特定方法,否则将要使用的模型粘合剂。所以,你可以在全球范围注册类模型粘结剂和使用属性覆盖它在一个特定的操作方法。

    One way of using the attribute in the method parameters could be overriding for that particular method the model binder that would otherwise be used. So you could globally register a model binder for your class and override it in one particular action method using the attribute.

    在年底有选择模型绑定相当多的选择。
    在ASP MVC 3,这将通过以下方式来解决(假设你使用的是默认ControllerActionInvoker)

    In the end there are quite a few options for selecting the model binder. In asp MVC 3 this will be resolved in the following way (assuming you are using the default ControllerActionInvoker)


    1. 上操作的参数的属性。见的<一个GetParameterValue方法href=\"https://github.com/ASP-NET-MVC/ASP.NET-Mvc-3/blob/master/mvc3/src/SystemWebMvc/Mvc/ControllerActionInvoker.cs\">ControllerActionInvoker类

    粘合剂,使用了IModelBinderProvider返回。请参见<一GetBinder方法href=\"https://github.com/ASP-NET-MVC/ASP.NET-Mvc-3/blob/master/mvc3/src/SystemWebMvc/Mvc/ModelBinderDictionary.cs\">ModelBinderDictionary类

    The Binder returned from the IModelBinderProvider. See GetBinder method in the ModelBinderDictionary class

    在活页夹中的ModelBinders.Binders字典全局注册。

    The Binder globally registered in the ModelBinders.Binders dictionary.

    在为模型类型 [ModelBinder的()] 属性定义的活页夹。

    The Binder defined in the [ModelBinder()] attribute for the model type.

    该DefaultModelBinder。

    The DefaultModelBinder.

    这篇关于使用ModelBinder的属性与ModelBinders.Add()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆