Asp.net MVC使用Model Binding解除Session, Cookie等依赖

上篇文章"Asp.net MVC使用Filter解除Session, Cookie等依赖"介绍了如何使用Filter来解除对于Session, Cookie的依赖。
其实这个也可以通过Model Binding来达到同样的效果。

什么是Model Binding?

Model Binding的作用就是将Request请求中包含的散乱参数,根据接受请求的Action方法的参数列表,自动智能地构建这些参数的过程。

 modelbinding

问题分析

常见的对于Session依赖的代码:

复制代码
public ActionResult Index()
{var user = Session[“UserAccuont”];//从Session中获取当前登录用户的信息//send emailvar email = user.Email;…………
}
复制代码

 

我们期望将函数改造成:

复制代码
public ActionResult Index(UserAccount sessionUser)
{//send emailvar email = sessionUser.Email;…………
}
复制代码

让Index action方法不用在方法内访问Session, 从而解除对于Session的依赖。
如果MVC能够智能的构造参数sessionUser, 从session取的数据,就能够解决我们的问题了。这就需要我们的Model Binding出场了。

自定义Model Binding

继承接口IModelBinder, 实现BindModel方法。
这个UserAccountModelBinder的作用就是从Session中取得UserAccount.

复制代码
public class UserAccountModelBinder : IModelBinder{public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext){if(controllerContext.HttpContext.Session["UserAccuont"] != null){return controllerContext.HttpContext.Session["UserAccuont"];}return null;}}
复制代码

 接下来,我们要为这个ModelBinder,添加到MVC的Model Binding设置中,使得它能够在MVC的生命周期中起作用。
在Global.asax.cs文件的Application_Start()方法中,添加UserAccountModelBinder

复制代码
protected void Application_Start()
{………//凡是UserAccount类型的参数,都会使用UserAccountModelBinder来处理,也就是会从Session中取值ModelBinders.Binders.Add(typeof(UserAccount), new UserAccountModelBinder ());}
复制代码

 上面就是全部的Model Binding解决问题的过程,希望能够帮助大家更好地理解MVC中的Model Binding.

转载于:https://www.cnblogs.com/webenh/p/7691744.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/280104.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

火狐 新增标签 一直加载_在Firefox的新标签页中加载最后标签页的URL

火狐 新增标签 一直加载Yeah, you’re pretty sure that you’re the master of all things Firefox. I mean, why else would you be reading this article? So, we’ve got to ask, have you ever seen this one before? 是的,您很确定自己是Firefox的所有人。 …

ptyhon【递归练习】

转载于:https://www.cnblogs.com/LTEF/p/9187287.html

JAVA常量

2019独角兽企业重金招聘Python工程师标准>>> 常量就是一个固定值。它们不需要计算,直接代表相应的值。 常量指不能改变的量。 在Java中用final标志,声明方式和变量类似: final double PI 3.1415927; 虽然常量名也可以用小写&…

基于Docker托管Azure DevOps代理

Azure DevOps非常好用,但是为代理准备单独的服务器经常会显得性价比不高:配置低了,前端构建时会教会你做人,配置太高又有点浪费资源,代理数量少了各团队构建要打架。对于既想享受DevOps的美妙之处但是资源捉襟见肘的小…

微软 word转换pdf_如何将行转换为Microsoft Word表中的列

微软 word转换pdfYou’ve created a table in Word and started to enter your data. Then, you realize that the table should be transposed, meaning the rows should be columns and vice versa. Rather than recreating the table and manually entering the data again,…

pycharm中如何正确配置pyqt5

网上看了几个文章,不成功。这样做才是正确姿势: /Users/mac/anaconda3/bin/Designer.app /Users/mac/anaconda3/bin$ProjectFileDir$ pyuic5 $FileName$ -o $FileNameWithoutExtension$.py $ProjectFileDir$ 其它细节懒得说。 转载于:https://www.cnblog…

如何在Photoshop中制作双曝光图像

Double exposure images are popular at the moment. Taylor Swift’s Style music video and the True Detective opening theme both used the effect. It’s a technique where two separate photos—typically a portrait and a landscape—are blended together into one …

记一次.NET 某安全生产系统 CPU爆高分析

一:背景 1.讲故事今天是🐏的第四天,头终于不巨疼了,写文章已经没什么问题,赶紧爬起来写。这个月初有位朋友找到我,说他的程序出现了CPU爆高,让我帮忙看下怎么回事,简单分析了下有两点…

JDBC 学习笔记(一)—— JDBC 基础

1. 什么是 JDBC JDBC,Java Database Connectivity(Java 数据库连接),是一组执行 SQL 语句的 Java API。 JDBC,是 Java SE(Java Platform, Standard Edition)标准的一部分。 Java 程序可以通过 J…

JavaScript享元模式

JavaScript享元模式 通过两个例子的对比来凸显享元模式的特点:享元模式是一个为了提高性能(空间复杂度)的设计模式,享元模式可以避免大量非常相似类的开销。 第一实例,没有使用享元模式,计算所花费的时间和空间使用程度。 要求为&…

mac屏幕截图_如何在Mac上拍摄屏幕截图

mac屏幕截图On a Mac, you can take screenshots with a few quick keyboard shortcuts. But Mac OS X also includes more powerful screenshot tools, too. Here are some of the many ways you can get a screenshot on OS X. 在Mac上,您可以使用一些快速的键盘快…

新手AS常见问题集锦

开发环境 以前开发android的时候可以使用eclipse,虽然现在也能使用eclipse,但是google已经不再支持使用eclipse开发android了。因为google有了自己的IDE---android studio,这个IDE我自己认为安装的时候比较方便,唯一的缺点就是在下…

也许你曾经读过他的书

我们愿用“能理能文、才华多元”来形容他。因为热爱编程和游戏,所以他将爱好变成了职业,并在这条路上持续奔跑;因为热爱分享,所以他坚持在博客上分享技术观点并出版了关于 Azure、微软游戏栈的书籍;因为热爱挑战&#…

t30智能插座怎么设置_如何设置ConnectSense智能插座

t30智能插座怎么设置If you like the idea of smart outlets, but wish you had one with more than just one receptacle on it, the ConnectSense Smart Outlet is worth looking into. Here’s how to set it up and instantly get double the fun. 如果您喜欢智能插座的想法…

用链表和数组实现HASH表,几种碰撞冲突解决方法

Hash算法中要解决一个碰撞冲突的办法,后文中描述了几种解决方法。下面代码中用的是链式地址法,就是用链表和数组实现HASH表。 he/*hash table max size*/ #define HASH_TABLE_MAX_SIZE 40/*hash table大小*/ int hash_table_size0;/*.BH----------------…

如何在Chrome中保存您当前的所有标签,以便以后阅读

Chrome allows you to open tabs from your last browsing session when you open the browser. However, what if you want to save your current set of tabs to re-open at any time? Chrome doesn’t provide a way to do that natively, but there is an easy workaround…

ubuntu 16.04(Windows 10双系统+grub引导)无法进入tt1~tt6(NVIDIA驱动安装相关-黑屏,login loop,分辨率)...

目录 前言回顾最终解决:0.关闭x服务1.禁用nouveau2.加入3.更新4.查找匹配驱动5.选择推荐版本6.等待安装后重启,nvidia-smi查看是否安装成功,或者lsmod | grep nvidia,成功结果如下7.重启x服务8.此时还不能进入图形界面,因为nomodeset还在&…

(备忘)打开office2010总是在配置进度

1、同时按上键盘上面的windows键和R键,出现“运行” 2、输入“regedit”,回车进入注册表 3、点击“HKEY_CURRENT_USER”展开,依次“Software”--“Microsoft”--“Office”--"14.0"--"Word"展开,点击"Op…

android 更改软键盘_如何在Android的Google键盘上更改声音和振动

android 更改软键盘Tactile feedback from a touch screen keyboard is crucial, in my opinion, but I don’t like sounds when I tap keys. You may not be like me—maybe sounds are your thing, but vibration is annoying. Or maybe you dislike both (you rebel!). The…

知识点:Mysql 索引原理完全手册(1)

知识点:Mysql 索引原理完全手册(1) 知识点:Mysql 索引原理完全手册(2) 知识点:Mysql 索引优化实战(3) 知识点:Mysql 数据库索引优化实战(4) Mysql-索引原理完全手册 一、 介绍二、 索引的原理三、 索引的数据结构四、 聚集索引与辅…