Asp.net Mvc (Filter及其执行顺序)
应用于Action的Filter
在Asp.netMvc中当你有以下及类似以下需求时你可以使用Filter功能
判断登录与否或用户权限,决策输出缓存,防盗链,防蜘蛛,本地化设置,实现动态Action
filter是一种声明式编程方式,在Asp.net MVC中它只能应用在Action上
Filter要继承于ActionFilterAttribute抽象类,并可以覆写void OnActionExecuting(FilterExecutingContext)和
void OnActionExecuted(FilterExecutedContext)这两个方法
OnActionExecuting是Action执行前的操作,OnActionExecuted则是Action执行后的操作
下面我给大家一个示例,来看看它的的执行顺序
首先我们先建立 一个Filter,名字叫做TestFilter
using System.Web.Mvc;
namespace MvcApplication2.Controllers
{
public class TestFilter : ActionFilterAttribute
{
public override void OnActionExecuting(FilterExecutingContext
filterContext) {
filterContext.HttpContext.Session["temp"] += "OnActionExecuting<br/>";
}
public override void OnActionExecuted(FilterExecutedContext
filterContext) {
filterContext.HttpContext.Session["temp"] += "OnActionExecuted<br/>";
}
}
}
在这里我们在Session["temp"]上标记执行的顺序
我们在Controller中的Action中写以下代码
[TestFilter]
public void Index() {
this.HttpContext.Session["temp"] += "Action<br/>";
RenderView("Index");
}在这个Action执行的时候,我们也为Session["temp"]打上了标记.
最后是View的内容
很简单我们只写
<%=Session["temp"] %>这样我们就可以执行这个页面了
在第一次执行完成后,页面显示
Action
这证明是先执行了OnActionExecuting然后执行了Action,我们再刷新一下页面
则得到
OnActionExecuting
Action
OnActionExecuted
OnActionExecuting
Action这是因为OnActionExecuted是在第一次页面 显示后才执行,所以要到第二次访问页面时才能看到
Controller的Filter
Monorail中的Filter是可以使用在Controller中的,这给编程者带来了很多方便,那么在Asp.net MVC中可不可以使用Controller级的Filter呢.不言自喻.
实现方法如下Controller本身也有OnActionExecuting与OnActionExecuted方法 ,将之重写即可,见代码
namespace MvcApplication2.Controllers
{
using System.Web.Mvc;
public class EiceController : Controller
{
public void Index() { this.HttpContext.Session["temp"] += "Action<br/>";
RenderView("Index");
}
public void Index2() {
RenderView("Index");
}
protected override void OnActionExecuting(FilterExecutingContext
filterContext) {
filterContext.HttpContext.Session["temp"] += "OnActionExecuting<br/>";
}
protected override void OnActionExecuted(FilterExecutedContext
filterContext) {
filterContext.HttpContext.Session["temp"] += "OnActionExecuted<br/>";
}
}
}这里要注意一点,这两个方法 一定要protected
要是想多个Controller使用这个Filter怎么办?继承呗.
Filter的具体生存周期
这是官方站的一数据.
来自controller虚方法 的OnActionExecuting .
应用于当前Controller的Filter中的OnActionExecuting:
先执行基类的,后执派生类的
执行应用于Action的Filter的OnActionExecuting顺序:
先执行基类的,后执派生类的
Action 方法
应用于Action的Filter的OnActionExecuted 的执行顺序
先执行派生类的,后执行基类的
应用于当前Controller的Filter中的OnActionExecuted方法
先执行派生类的,后执行基类的
Controller中的虚方法 OnActionExecuted
示例可见http://quickstarts.asp.net/3-5-extensions/mvc/ActionFiltering.aspx
Asp.net Mvc Framework 系列
Asp.net Mvc (Filter及其执行顺序)的更多相关文章
- ASP.NET MVC ActionFilterAttribute的执行顺序
http://diaosbook.com/Post/2014/6/3/execution-order-of-actionfilter-aspnet-mvc ASP.NET MVC里面我们要自定义Act ...
- ASP.NET MVC应用程序执行过程分析
ASP.NET MVC应用程序执行过程分析 2009-08-14 17:57 朱先忠 朱先忠的博客 字号:T | T ASP.NET MVC框架提供了支持Visual Studio的工程模板.本文 ...
- filter的执行顺序
一直没有仔细去研究下filter ,最近系统的测试了下: 先看代码吧 FirstFilter.java ================== package com.test.filter; impo ...
- Servlet 3.0 之@WebFilter怎么控制多个filter的执行顺序
之前我们控制多个filter的执行顺序是通过web.xml中控制filter的位置来控制的,放在上面的会比放在下面的先执行,如下“用户登录检查过滤器”会比“接口日志过滤器”先执行 <!-- ...
- 04_过滤器Filter_03_多个Filter的执行顺序
[Filter链] *在一个web应用中,可以开发编写多个Filter,这些Filter组合起来称为一个Filter链. *web服务器根据Filter在web.xml中的注册顺序,决定先调用哪个Fi ...
- 拦截器的四种拦截方式以及Filter的执行顺序(17/4/8)
一:拦截方式 需要在配置文件web.xml配置 在对应filter-mapping节点下 如下 <filter-mapping> <filter-name>BFilter< ...
- @WebFilter怎么控制多个filter的执行顺序
转自:http://blog.csdn.net/liming_0820/article/details/53332070 之前我们控制多个filter的执行顺序是通过web.xml中控制filter的 ...
- java-过滤器Filter_多个Filter的执行顺序
http://www.cnblogs.com/HigginCui/p/5772514.html [Filter链] *在一个web应用中,可以开发编写多个Filter,这些Filter组合起来称为一个 ...
- 转:Filter的执行顺序与实例
转:http://www.cnblogs.com/Fskjb/archive/2010/03/27/1698448.html Filter的执行顺序与实例 Filter介绍 Filter可认为是Ser ...
随机推荐
- 拆解探索MagSafe电源接口结构和指示灯变颜色原理
你有没有想过一个Mac的MagSafe接头里面有什么? 控制光线是什么? 在Mac如何知道它是什么样的充电器? 本文探讨的MagSafe连接器内,并回答这些问题. 2006年由苹果公司推出的MagSa ...
- 用Qemu模拟vexpress-a9 (六) --- 多核
环境介绍 Win7 64 + Vmware 11 + ubuntu14.04 32 u-boot 版本:u-boot-2015-04 Linux kernel版本:linux-3.16.y busyb ...
- MultipartFile+nio上传文件
import java.io.File;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths; ...
- A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28
上面回答有问题,找到qq官方的文档了 http://service.exmail.qq.com/cgi-bin/help?id=28&no=1000585&subtype=1 如果您的 ...
- Robots协议(爬虫协议、机器人协议)
Robots协议(也称为爬虫协议.机器人协议等)的全称是“网络爬虫排除标准”(Robots Exclusion Protocol),网站通过Robots协议告诉搜索引擎哪些页面可以抓取,哪些页面不能抓 ...
- [13] 弧面(Arc)图形的生成算法
顶点数据的生成 bool YfBuildArcVertices ( Yreal radius, Yreal degree, Yreal height, Yuint slices, Yuint stac ...
- fstream的使用方法
ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O ...
- Informatica 常用组件Source Qualifier之八 Distinct
如果希望 PowerCenter 从源选择唯一值,您可以使用"选择相异"选项.例如,您可以使用此功能从列出总销售额的表中提取唯一客户标识.使用"选择相异"过滤器 ...
- Python编程学习,高效求解素数程序实例
素数是编程中经常需要用到的. 作为学习Python的示例,下面是一个高效求解一个范围内的素数的程序,不需要使用除法或者求模运算. #coding:utf-8 #设置python文件的编码为utf-8, ...
- Gradle for Android 翻译 -1
英文版电子书下载 参考:Gradle for Android 一.从 Gradle 和 AS 开始 [Getting Started with Gradle and Android Studio] ...