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 ...
随机推荐
- C++回调函数(callback)的使用
什么是回调函数(callback) 模块A有一个函数foo,他向模块B传递foo的地址,然后在B里面发生某种事件(event)时,通过从A里面传递过来的foo的地址调用foo,通知A发生了什么事 ...
- fundamentals5
PROTOBUF的DELPHI开源框架fundamentals5 GITHUB: https://github.com/fundamentalslib/fundamentals5 # Fundamen ...
- no scheme 问题
用xcode4打开xcode3建立的工程,有时候,不能自动转换版本,就会显示no scheme. 这个是由于XXX..xcodeproj包中xcuserdata文件夹中user.xcuserdatad ...
- style="visibility: hidden"和 style=“display:none”之间的区别
style=“display:none” 隐藏页面元素: <html> <head> <script type="text/javascript"&g ...
- [Linux] Systemd 入门教程:命令篇
reference : http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html Systemd 是 Linux 系统 ...
- RHPAM 7.2安装
1.产品架构 2.安装过程 下载相应介质 JBoss EAP (jboss-eap-7.2.0.zip) 下载地址 https://developers.redhat.com/products/e ...
- 最小二乘法least square
上研究生的时候接触的第一个Loss function就是least square.最近又研究了一下,做个总结吧. 定义看wiki就够了.公式如下 E(w)=12∑n=1N{y−xWT}2E(w)=12 ...
- 【BZOJ】【3771】Triple
生成函数+FFT Orz PoPoQQQ 这个题要算组合的方案,而且范围特别大……所以我们可以利用生成函数来算 生成函数是一个形式幂级数,普通生成函数可以拿来算多重集组合……好吧我承认以上是在瞎扯→_ ...
- 系统对接API调用
在与公司外部系统对接时,API接口一般采用REST风格,对外暴露HTTP服务.只需要将入参封装好,并发起HTTP请求即可.具体请求流程如下图所示: 数据格式 API调用参数分为系统参数和业务参数,请求 ...
- Informatica 常用组件Lookup之一 概述
转换类型:被动.已连接/未连接 在映射中使用查找转换以从平面文件或关系表.视图或同义词查找数据.您可以从 PowerCenter Client 和 PowerCenter Server 均连接至的任何 ...