ASP.NET MVC5 ModelBinder
什么是ModelBinding
ASP.NET MVC中,所有的请求最终都会到达某个Controller中的某个Action并由该Action负责具体的处理和响应。为了能够正确处理请求,Action的参数(如果有的话),必须在Action执行之前,根据相应的规则,把请求中所包含的数据提取出来并将映射为Action的参数值,这个过程就是ModelBinding。ModelBinding的作用就是为Action提供参数列表。
ModelBinding的好处
- 使代码变得更加简洁
- 帮助我们获取HTTP请求中的数据
- 帮助我们完成必要的数据类型转换
ASP.NET MVC中ModelBinding的实现过程
ASP.NET MVC中ModelBinding的实现过程比较复杂,这里简要说明它的总体流程。具体的实现过程可以看蒋金楠的《ASP.NET MVC5框架揭秘》或者看他的博客How ASP.NET MVC Works?,讲解很详细。
- HTTP请求中的数据可能存在于querystring中,也可能在表单中,也有可能是JSON字符串。究竟从哪里获取数据,这要依赖于参数的描述信息
ParameterDescriptor
ParameterDescriptor
的获取需要借助于ControllerDescriptor
和ActionDescriptor
,它们分别用来描述Controller和ActionIModelBinderProvider
用于提供合适的ModelBinder对象,我们可以自己实现该接口以获取自定义的IModelBinder
- ModelBinding的核心是
IModelBinder
,默认实现类是DefaultModelBinder
,我们可以自己实现IModelBinder
接口来扩展ModelBinder IValueProvider
针对不同的数据源提供了数据的访问机制ValueProviderResult
提供了两个ConvertTo方法重载以实现向指定目标类型的转换。- 经过上述一系列的处理获取最终结果
自定义ModelBinder
自定义Modelbinder只需实现System.Web.Mvc.IModelBinder
接口即可。这里需要注意一点,System.Web.ModelBinding
命名空间下也有一个IModelBinder
接口,不要搞错了。
public class LessonEditInfoViewModelBinder : IModelBinder {//根据前台传递的id值获取对象public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext){var idStr = controllerContext.HttpContext.Request["id"] ?? controllerContext.RouteData.Values["id"]?.ToString();int id = 0;if (!int.TryParse(idStr, out id)){return null;}var model = new LessonBLL().GetLessonEditInfo(id);return model;} }
然后使用ModelBinderAttribute
进行标注即可:
/*根据前台传递的id值解析出对象数据,Action无需关注对象的获取,使代码变得清晰简洁 */ public ActionResult Edit([ModelBinder(typeof(LessonEditInfoViewModelBinder))]LessonEditInfoViewModel lesson) {if (lesson == null){//跨控制器的视图跳转要使用视图的路径+文件名return View("/Views/Exception/GenericError.cshtml", new ExceptionViewModel { Title = "404", Description = "课程不存在!" });}return View(lesson); }
如果项目中多处需要使用自定义的ModelBinder,那么再使用ModelBinderAttribute
进行标注就不大合适了。这种情况我们可以使用自定义的ModelBinderProvier。代码如下:
public class CustomeModelBinderProvider : IModelBinderProvider {public IModelBinder GetBinder(Type modelType){if (modelType == typeof(LessonEditInfoViewModel)){return new LessonEditInfoViewModelBinder();}return null;} }
然后将自定义的ModelBinderProvider注册到ASP.NET MVC系统中
public class MvcApplication : System.Web.HttpApplication {protected void Application_Start(){ModelBinderProviders.BinderProviders.Insert(0, new CustomeModelBinderProvider());} }
完成上述两步之后,就无需使用ModelBuilderAttribute
进行标注了。
除此之外,还可在Global文件中使用使用ModelBinder
类的Binder
属性来注册ModelBinderProvider
ModelBinders.Binders.Add(typeof(LessonEditInfoViewModel),new LessonEditInfoViewModelBinder());
不同的ModelBinder提供策略有不同的优先级,具体如下:
- 在参数上使用CustomModelBinderAttribute
- 使用ModelBinderProviders.BinderProviers
- 使用ModelBinders.Binders
- 参数类型上标记CustomModelBinderAttribute
- ASP.NET MVC中提供的DefaultModelBinder
注意,CustomModelBinderAttribute是抽象类,在ASP.NET MVC中有唯一子类ModelBinderAttribute。
参考文章:
Model Binders in ASP.NET MVC
ModelBinder——ASP.NET MVC Model绑定的核心
ASP.NET MVC以ValueProvider为核心的值提供系统
玩转Asp.net MVC 的八个扩展点
ASP.NET MVC中你必须知道的13个扩展点
版权声明
本文为作者原创,版权归作者雪飞鸿所有。 转载必须保留文章的完整性,且在页面明显位置处标明原文链接。
如有问题, 请发送邮件和作者联系。
转载于:https://www.cnblogs.com/Cwj-XFH/p/5977508.html
ASP.NET MVC5 ModelBinder相关推荐
- ASP.NET MVC5 高级教程 学习笔记
// 本应用是ASP.NET MVC5 高级教程学习产物 // 2015-07-23 // 第一章 入门 // 第二章 控制器 // 第三章 视图 1.Install-Package Wrox.Pro ...
- [Asp.net MVC]Asp.net MVC5系列——添加数据
目录 概述 显示添加数据时所用表单 处理HTTP-POST 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列--第一个项目 [Asp.net MVC]Asp.net MVC5系列 ...
- [Asp.net MVC]Asp.net MVC5系列——第一个项目
目录 概述 创建第一个项目 添加控制器 总结 概述 本教程是个人一步一步学习的总结,希望能帮到正在进入ASP.Net MVC5方向的朋友,个人也是准备进入ASP.NET MVC5领域,虽然艰辛,但是乐 ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](三)
前言 上一篇<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](二)>我们通过如下操作: 创建实体及工具类 创建Re ...
- ASP.NET MVC5 高级编程 第3章 视图
参考资料<ASP.NET MVC5 高级编程>第5版 第3章 视图 3.1 视图的作用 视图的职责是向用户提供界面. 不像基于文件的框架,ASP.NET Web Forms 和PHP ,视 ...
- ASP.NET MVC5微信公众平台整合开发实战教程
<ASP.NET MVC5&微信公众平台整合开发实战(响应式布局.JQuery Mobile,Windows Azure.微信核心开发)> 课程讲师:57Code 课程分类:ASP ...
- ASP.NET MVC5+ 路由特性
概述 ASP.NET MVC 5支持一种新的路由协议,称为路由特性. MVC5也支持以前定义路由的方式,你可以在一个项目中混合使用这两种方式来定义路由. 案例 1.使用Visual Studio 20 ...
- ASP.NET MVC5 + EF6 入门教程 (6) View中的Razor使用
ASP.NET MVC5 + EF6 入门教程 (6) View中的Razor使用 原文:ASP.NET MVC5 + EF6 入门教程 (6) View中的Razor使用 文章来源: Slark.N ...
- ASP.NET MVC5 网站开发实践(二) Member区域 - 用户部分(2)用户登录、注销
上次实现了用户注册,这次来实现用户登录,用到IAuthenticationManager的SignOut.SignIn方法和基于声明的标识.最后修改用户注册代码实现注册成功后直接登录. 目录: ASP ...
最新文章
- Vue项目中使用wangEditor富文本输入框(推荐)
- Linux原始套接字实现分析---转
- IREP_SOA Integration程序注释语法Annotations(概念)
- 【小白学PyTorch】14.tensorboardX可视化教程
- HashMap底层实现原理/HashMap与HashTable区别/HashMap与HashSet区别
- python整形魔法_python 魔法方法
- 【电路仿真】基于simulink全数字锁相环设计【含Matlab源码 340期】
- python气象包_Python-Cartopy包: 地图投影
- SLF4J user manual
- MacBook Pro 进入屏保 发热严重
- excel常用函数公式
- G33/G31芯片最佳分辨率1440×900
- 微信小程序模拟器里面不能显示自己写的INDEX
- 解决可以上QQ,但是无法上网的问题
- 用VBA实现对一维数组的排序(7)堆排序
- vue中样式穿透的三种写法
- VOSviewer进行中文和英文文献分析
- zjs-my-diary-0220118
- U盘无法打开提示格式化解决方案
- 联发科MT6797/x20开发板设计,MT6797方案定制,MT6797芯片模块资料
热门文章
- 【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】 小乐乐算数字(水题,快速幂,lowbit)
- 【 HDU - 2594 】Simpsons’ Hidden Talents(KMP应用,求最长前缀后缀公共子串)
- 【CodeForces - 764B 】Timofey and cubes (模拟)
- Apollo进阶课程㉝丨Apollo ROS原理—2
- java8 nio_Java8之 NIO的学习
- java socket 判断是否断开_Linux Socket:如何在客户端程序中检测断开的网络?
- pandas中size方法_如何使用pdpipe与Pandas构建管道?
- 设置角标_iPhone桌面角标颜色
- leetcode96. 不同的二叉搜索树 动归vs数学?
- GDB调试技术(二)