ASP.NET MVC中有四种过滤器类型
程序员文章站
2022-04-14 16:05:28
在ASP.NET MVC中有四种过滤器类型 Action 1、在ASP.NET MVC项目中,新建文件夹Filter,然后新建类MyCustormFilter,继承自ActionFilterAttribute类,我们来看下ActionFilterAttribute类有如下四个方法,从命名我应该就可以 ......
在asp.net mvc中有四种过滤器类型
action
1、在asp.net mvc项目中,新建文件夹filter,然后新建类mycustormfilter,继承自actionfilterattribute类,我们来看下actionfilterattribute类有如下四个方法,从命名我应该就可以看出他们的执行时机。
public class mycustormfilter:actionfilterattribute { public override void onactionexecuting(actionexecutingcontext filtercontext) { filtercontext.httpcontext.response.write("action执行前"); base.onactionexecuting(filtercontext); } public override void onactionexecuted(actionexecutedcontext filtercontext) { filtercontext.httpcontext.response.write("action执行后"); base.onactionexecuted(filtercontext); } }
2、对于过滤器,我们可以把它们加在三个地方,一个是控制器上面(控制器下面的所有action),一个是action上面(指定标识的action),另一个就是全局位置(所有控制器中的action)。这里我只演示在action上面加
[mycustormfilter]
public actionresult index()
{
return view();
}
public actionresult index1()
{
return view();
}
3、build然后运行
result
1、新建类testresultfilter,继承actionfilterattribute
public class testresultfilter:actionfilterattribute { /// <summary> /// 加载 "视图" 前执行 /// </summary> /// <param name="filtercontext"></param> public override void onresultexecuting(system.web.mvc.resultexecutingcontext filtercontext) { filtercontext.httpcontext.response.write("加载视图前执行 onresultexecuting <br/>"); base.onresultexecuting(filtercontext); } /// <summary> /// 加载"视图" 后执行 /// </summary> /// <param name="filtercontext"></param> public override void onresultexecuted(system.web.mvc.resultexecutedcontext filtercontext) { filtercontext.httpcontext.response.write("加载视图后执行 onresultexecuted <br/>"); base.onresultexecuted(filtercontext); } }
2、这里我把testresultfilter过滤器加在控制器上面
[testresultfilter] public class filtertestcontroller : controller { [mycustormfilter] public actionresult index() { return view(); } public actionresult index1() { return view(); } }
3、运行看下结果
注意:result过滤器,无论action的返回类型为什么(甚至void)都将执行。
routedata中保存了当前请求匹配的路由信息和路由对象
修改mycustormfilter.cs
public override void onactionexecuting(actionexecutingcontext filtercontext) { //1.获取获取请求的类名和方法名 string strcontroller = filtercontext.routedata.values["controller"].tostring(); string straction = filtercontext.routedata.values["action"].tostring();//2.另一种方式 获取请求的类名和方法名 string straction2 = filtercontext.actiondescriptor.actionname; string strcontroller2 = filtercontext.actiondescriptor.controllerdescriptor.controllername; filtercontext.httpcontext.response.write("action执行前</br>"); filtercontext.httpcontext.response.write("控制器:" + strcontroller + "</br>"); filtercontext.httpcontext.response.write("控制器:" + strcontroller2+"</br>"); filtercontext.httpcontext.response.write("action:" + straction + "</br>"); filtercontext.httpcontext.response.write("action:" + straction2 + "</br>"); base.onactionexecuting(filtercontext); }
authorizeattribute
1、 新建testauthorizeattribute
/// <summary> /// 授权过滤器 --在action过滤器前执行 /// </summary> public class testauthorizeattribute:authorizeattribute { public override void onauthorization(authorizationcontext filtercontext) { filtercontext.httpcontext.response.write("<br/>onauthorization<br/>"); //注释掉父类方法,因为父类里的 onauthorization 方法会调用asp.net的授权验证机制! //base.onauthorization(filtercontext); } }
2、在控制器filtertest中的index上添加testauthorize标记
[mycustormfilter] [testauthorize] public actionresult index() { return view(); }
运行看下结果:
exception
1、新建testhandleerror.cs
/// <summary> /// 异常处理 过滤器 /// </summary> public class testhandleerror : handleerrorattribute { public override void onexception(exceptioncontext filtercontext) { //1.获取异常对象 exception ex = filtercontext.exception; //2.记录异常日志 //3.重定向友好页面 filtercontext.result = new redirectresult("~/error.html"); //4.标记异常已经处理完毕 filtercontext.exceptionhandled = true; base.onexception(filtercontext); } }
2、在action上面加testhandleerror
[testhandleerror] public actionresult geterr() { int a = 0; int b = 1 / a; return view(); }
注意:通常这样的异常处理我们是放在全局过滤器上面的。
public class filterconfig { public static void registerglobalfilters(globalfiltercollection filters) { //filters.add(new handleerrorattribute()); //添加全局过滤器 filters.add(new testhandleerror()); } }