模型绑定器

之前或多或少也提到过模型绑定器,方法的形参就是由模型绑定器把参数绑定上去的,今天就说说ModuleBingder那点事

在MVC中有一个接口叫IModuleBinder

//
// 摘要:
// Defines the method that is required for a model binder.
public interface IModelBinder
{
//
// 摘要:
// Binds the model to a value by using the specified execution context and binding
// context.
//
// 参数:
// modelBindingExecutionContext:
// The execution context.
//
// bindingContext:
// The binding context.
//
// 返回结果:
// true if model binding is successful; otherwise, false.
bool BindModel(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext);
}

当然,这是接口,是没有实现的,MVC给我们提供了相应的实现类。DefaultModelBinder,所有的形参都是通过它去处理的。

下面是源码,这里可以看到有两个重要的方法  BindSimpleModel、BindComplexModel 简单类型绑定与复杂类型的绑定,实际上方法是递归调用的,如果参数是对象的话,那就会创建一个对象,然后在绑定每一个属性,而绑定每一个属性的方法里又调用了BindModel方法。

public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
RuntimeHelpers.EnsureSufficientExecutionStack();
if (bindingContext == null)
{
throw new ArgumentNullException("bindingContext");
}
bool flag = false;
if (!string.IsNullOrEmpty(bindingContext.ModelName) && !bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
{
if (!bindingContext.FallbackToEmptyPrefix)
{
return null;
}
ModelBindingContext context = new ModelBindingContext {
ModelMetadata = bindingContext.ModelMetadata,
ModelState = bindingContext.ModelState,
PropertyFilter = bindingContext.PropertyFilter,
ValueProvider = bindingContext.ValueProvider
};
bindingContext = context;
flag = true;
}
if (!flag)
{
bool flag2 = ShouldPerformRequestValidation(controllerContext, bindingContext);
ValueProviderResult valueProviderResult = bindingContext.UnvalidatedValueProvider.GetValue(bindingContext.ModelName, !flag2);
if (valueProviderResult != null)
{
return this.BindSimpleModel(controllerContext, bindingContext, valueProviderResult);
}
}
if (!bindingContext.ModelMetadata.IsComplexType)
{
return null;
}
return this.BindComplexModel(controllerContext, bindingContext);
}
protected virtual object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
{
object objA = propertyBinder.BindModel(controllerContext, bindingContext);
if (bindingContext.ModelMetadata.ConvertEmptyStringToNull && object.Equals(objA, string.Empty))
{
return null;
}
return objA;
}

MVC是高度可替换的,如果DefaultModelBinder无法很好的实现我们的需求,那么可以写自己的ModelBinder。首先定义一个类去继承自IModelBinder实现BindModel方法,然后在Global里替换我们的MyModelBinder

public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
//替换
ModelBinders.Binders.DefaultBinder = new MyModelBinder();
} public class MyModelBinder : IModelBinder
{
public object BindModel(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext)
{
//Todo...
}
}
}

MVC5-10 ModleBinder那点事的更多相关文章

  1. InnoDB 存储引擎的锁机制

    测试环境隔离级别:REPEATABLE-READ 行级别的 - Share and Exclusive Locks 共享锁 S:允许持有S锁的事务对行进行读操作 排他锁 X: 允许持有X锁的事务对行进 ...

  2. Javascript parseFloat内部解析规则

    这是由小习发的一个问题引起的讨论,结束后大家各自加深了多parseFloat的理解. 如下 16进制数0x10使用parseFloat转成数字,结果为0.潜意识期望的结果是16. 有人说脑残,16进制 ...

  3. Scipy教程 - 统计函数库scipy.stats

    http://blog.csdn.net/pipisorry/article/details/49515215 统计函数Statistical functions(scipy.stats) Pytho ...

  4. 2018 AI产业界大盘点

    2018  AI产业界大盘点 大事件盘点 “ 1.24——Facebook人工智能部门负责人Yann LeCun宣布卸任 Facebook人工智能研究部门(FAIR)的负责人Yann LeCun宣布卸 ...

  5. odoo:开源 ERP/CRM 入门与实践

    看了这张图,或许你对odoo有了一些兴趣. 这次就是和大家一起交流开源ERP/CRM系统:odoo 对以下读者有帮助:研发.产品.项目.市场.服务.运营.管理等. 一.背景趋势 社交网络.电商O2O: ...

  6. scipy.stats

    scipy.stats Scipy的stats模块包含了多种概率分布的随机变量,随机变量分为连续的和离散的两种.所有的连续随机变量都是rv_continuous的派生类的对象,而所有的离散随机变量都是 ...

  7. odoo:开源 ERP/CRM 入门与实践 -- 上海嘉冰信息技术公司提供咨询服务

    odoo:开源 ERP/CRM 入门与实践 看了这张图,或许你对odoo有了一些兴趣. 这次Chat就是和大家一起交流开源ERP/CRM系统:odoo 对以下读者有帮助:研发.产品.项目.市场.服务. ...

  8. Linux 小知识翻译 - 「为什么安全是互联网的问题?」

    当然,虽说「由于有心怀不轨的人在,一定要注意安全问题」.但另一方面,也有人认为「如果互联网自己就考虑好安全问题的话,那么用户就不用再担心安全问题了」. 虽然经常有人这样说「与远程机器通信的时候,避免使 ...

  9. 《Linux就是这个范儿》

    <Linux就是这个范儿> 基本信息 作者: 赵鑫磊    (加)Jie Zhang(张洁) 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115359360 上架时间:2 ...

随机推荐

  1. rpc框架之 thrift 学习 1 - 安装 及 hello world

    thrift是一个facebook开源的高效RPC框架,其主要特点是跨语言及二进制高效传输(当然,除了二进制,也支持json等常用序列化机制),官网地址:http://thrift.apache.or ...

  2. myeclipse中发送邮件出现Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream

    出现这个问题的原因是jar包版本不统一,解决方法如下: 我在项目导入了jar包 与myeclipse自带jar冲突了 删除Java EE 5 Libraries/javaee.jar/mail里的包有 ...

  3. Qt Creator 常用快捷键

    多行注释模式                                                                                            Ct ...

  4. opencv6.5-imgproc图像处理模块之轮廓

    接opencv6.4-imgproc图像处理模块之直方图与模板 这部分的<opencv_tutorial>上都是直接上代码,没有原理部分的解释的. 十一.轮廓 1.图像中找轮廓 /// 转 ...

  5. JavaScript精要

    写在开篇之前 这个系列都文章算是我最近研究了JavaScript(以后简称js)大半个月的一点心得吧.记得以前看过罗小平的一本书叫<Delphi精要>,我也就姑且起名叫<JavaSc ...

  6. 实现解耦-Spring.Net

    spring.net属于IOC(中文名:控制反转)的思想实现. 概念解释: 控制反转概念: 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来 ...

  7. 网页设计:Meta标签详解

    很多人忽视了HTML标签META的强大功效,一个好的META标签设计可以大大提高你的个人网站被搜索到的可能性,有兴趣吗,谁我来重新认识一下META标签吧! META标签是HTML语言HEAD区的一个辅 ...

  8. java多态实现原理

    众所周知,多态是面向对象编程语言的重要特性,它允许基类的指针或引用指向派生类的对象,而在具体访问时实现方法的动态绑定.C++ 和 Java 作为当前最为流行的两种面向对象编程语言,其内部对于多态的支持 ...

  9. js中奇特的for循环写法

    //正常的for循环 for(var i=0;i<10;i++){ console.log(i); } //输出:1,2,3……10 //简写 for(var i=10;i--;){ conso ...

  10. ubuntu14.04切换root用户

    打开命令窗口(CTRL+ALT+T),输入:sudo -s -->接着输入管理密码, -->已经切换到root用户