Jer*_*ose 23 c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4
有人能告诉我在global.asax中使用[ModelBinder()]属性与注册模型绑定器的优点/精确度ModelBinders.Add()吗?
我能想到的一个优点是它更明确,而在全局注册ModelBinders对于检查动作方法的人并不那么明显.
我能想到的一个权衡是它不可重复使用,因为您必须将此属性添加到需要使用此模型绑定器的所有操作方法,而在全局中注册ModelBinders将使其可用于接收该模型的所有操作方法.
这是唯一的区别吗?
换句话说,会说明这是正确的:
[ModelBinder()].ModelBinders.Dan*_*.G. 31
这些技术的结果将是相同的,因此主要是团队感到更舒服的问题.所以你可以提出一个像你所说的那样的约定.
就个人而言,我更喜欢不必在使用该模型的每个操作方法上设置属性.所以我会选择以下选项之一:
在模型类上设置属性,如:
[ModelBinder(typeof(MyModelBinder))]
public class MyModel
{
...
}
Run Code Online (Sandbox Code Playgroud)全球注册活页夹
ModelBinders.Binders.Add(typeof(MyModel), new MyModelBinder())
Run Code Online (Sandbox Code Playgroud)我更喜欢其中之一的另一个原因是因为如果您必须手动触发模型绑定过程,您可能还需要使用自定义模型绑定器:
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())
...
}
Run Code Online (Sandbox Code Playgroud)
您还可以选择实现自己的逻辑,通过实现接口IModelBinderProvider并在global.asax中注册来选择模型绑定器.
ModelBinderProviders.BinderProviders.Add(new CustomModelBinderProvider())
Run Code Online (Sandbox Code Playgroud)
在方法参数中使用属性的一种方法可能是覆盖否则将使用的模型绑定器的特定方法.因此,您可以为您的类全局注册模型绑定器,并使用该属性在一个特定的操作方法中覆盖它.
最后,有很多选择模型绑定器的选项.在asp MVC 3中,这将通过以下方式解决(假设您使用的是默认的ControllerActionInvoker)
动作参数的属性.请参阅ControllerActionInvoker类的 GetParameterValue方法
Binder从IModelBinderProvider返回.请参阅ModelBinderDictionary类中的GetBinder方法
Binder全局注册在ModelBinders.Binders字典中.
Binder在[ModelBinder()]模型类型的属性中定义.
DefaultModelBinder.
| 归档时间: |
|
| 查看次数: |
14725 次 |
| 最近记录: |