Asp.Net MVC在过滤器中使用模型绑定
废话不多话,直接上代码
1、创建MVC项目,新建一个过滤器类以及使用到的实体类:
public class DemoFiltersAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
var person = new Person();
//过滤器中使用模型绑定
BindModel<Person>(filterContext, person);
filterContext.Result = new JsonResult() { Data = person, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
//base.OnAuthorization(filterContext);
} /// <summary>
/// 模型绑定
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <param name="model">模型(绑定成功后将会给此赋值)</param>
/// <param name="prefix">获取或设置在呈现表示绑定到操作参数或模型属性的标记时要使用的前缀</param>
public void BindModel<TModel>(AuthorizationContext filterContext, TModel model, string prefix = "") where TModel : class
{
IModelBinder binder = ModelBinders.Binders.GetBinder(typeof(TModel));
ModelBindingContext bindingContext = new ModelBindingContext()
{
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(TModel)),
ModelName = prefix,
ValueProvider = filterContext.Controller.ValueProvider
};
binder.BindModel(filterContext.Controller.ControllerContext, bindingContext);
}
}
public class Person
{
public int Id { set; get; }
public string Name { set; get; }
}
继承了AuthorizeAttribute类中的OnAuthorization方法会在执行Action之前执行,具体可以查看我写的这篇文章《Asp.Net MVC过滤器》
2、新建一个控制类,并在控制器贴上DemoFilters特性:
[DemoFilters]
public class HomeController : Controller
{
// GET: Home
public ActionResult Index(Person p)
{
return Content("");
}
}
3、访问url:
http://localhost:8290/home/index?id=1&name=lisi
输出:{"Id":1,"Name":"lisi"}
Asp.Net MVC在过滤器中使用模型绑定的更多相关文章
- 4.ASP.NET MVC 5.0 视图之模型绑定
大家好,这篇文章,我将向大家介绍ASP.NET MVC的模型视图绑定,ASP.MVC的模型绑定分为两种:一种是动态绑定[Dynamic Binding];还有一种就是强类型绑定[Strongly ty ...
- ASP.NET Core 中文文档 第四章 MVC(2.1)模型绑定
原文:Model Binding 作者:Rachel Appel 翻译:娄宇(Lyrics) 校对:许登洋(Seay).何镇汐 模型绑定介绍 ASP.NET Core MVC 中的模型绑定从 HTTP ...
- Asp.net Mvc 中的模型绑定
asp.net mvc中的模型绑定可以在提交http请求的时候,进行数据的映射. 1.没有模型绑定的时候 public ActionResult Example0() { ) { string id ...
- 使用ASP.NET MVC操作过滤器记录日志(转)
使用ASP.NET MVC操作过滤器记录日志 原文地址:http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_Filte ...
- ASP.NET MVC : Action过滤器(Filtering)
http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html ASP.NET MVC : Action过滤器(Filtering) 相 ...
- Asp.net MVC 之过滤器
整理一下MVC中的几种过滤器,以及每种过滤器是干什么用的 四种过滤器 1.AuthorizationFilter(授权过滤器) 2.ActionFilter(方法过滤器) 3.ResultFilter ...
- [翻译] 使用ASP.NET MVC操作过滤器记录日志
[翻译] 使用ASP.NET MVC操作过滤器记录日志 原文地址:http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_ ...
- ASP.NET MVC 3: Razor中的@:和语法
原文 ASP.NET MVC 3: Razor中的@:和语法 [原文发表地址] ASP.NET MVC 3: Razor’s @: and <text> syntax[原文发表时间] De ...
- ASP.NET MVC开发学习过程中遇到的细节问题以及注意事项
1.datagrid中JS函数传值问题: columns: { field: 'TypeName', title: '分类名称', width: 120, sortable: true, format ...
随机推荐
- HDU 2131 Probability
http://acm.hdu.edu.cn/showproblem.php?pid=2131 Problem Description Mickey is interested in probabili ...
- 深入理解Java Web——Servlet
1.概述 狭义上看,是java的一个接口. 广义上看,任何直接或间接实现了Servlet接口的类. 2.核心对象 下面就是Servlet接口的内容: public abstract interface ...
- Ubuntu如何进入命令模式
Ctrl+Alt+T 或者Ctrl+Alt+F2~F6进入命命令模式 Ctrl+Alt+F7返回桌面
- linux下搜索find命令拾遗
强制删除项目下面的所有.svn文件目录,find . -name ‘.svn’ -exec rm -rf {} \; empty显示所有的空白文件,并显示详细:find . -empty size显示 ...
- LeetCode -- Tiangle
Question: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to ...
- sql如何先排序再去重
场景 有一张得分表(score),记录了用户每次的得分,同一个人可能有多个得分. id name score 1 tom 45 2 jack 78 3 tom 34 . . . 需求:找出分数最高的前 ...
- [APIO2018] New Home
题面在这里 description 在一个数轴上: 给定\(n\)个商店,每个商店有一个开业时间,关门时间,坐标和销售物品的种类 同时有\(m\)个询问,每个询问给你一个时间\(t[i]\)和地点\( ...
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 遇到问题---java---git下载的maven项目web用tomcat发布时不带子项目
遇到的情况是用git下载maven项目,然后用mvn eclipse:eclipse命令标记为eclipse项目之后,使用maven插件导入之后用tomcat发布运行,发现maven关联的几个子项目没 ...
- poj 3422 洛谷P2045 K取方格数(方格取数加强版)
Description: 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每达到一格,把该格子的数取出来 ...