MVC2里的拦截器接口(也叫过滤器接口):IActionFilter,IExceptionFilter,IResultFilter,并实现自己的拦截器。。。。
1、IActionFilter(Action拦截器接口)
Action拦截器分别在“执行Action之前”拦截和“执行Action之后”拦截,2个方法如下:
// Called after the action method executes.
//
// 参数:
// filterContext:
// The filter context.
void OnActionExecuted(ActionExecutedContext filterContext);
//
// 摘要:
// Called before an action method executes.
//
// 参数:
// filterContext:
// The filter context.
void OnActionExecuting(ActionExecutingContext filterContext);
看摘要就可以区分出哪个是执行之前拦截和执行之后拦截了,我们只需要实现这个接口就可以在执行Action之前做些处理和执行Action之后做些“善后”处理了。。。。
参数:filterContext 包含了执行整个Action的内容,我们可以取到我们想要的内容,如Action名称啦,一些参数等等。。。
示例代码如:
{
filterContext.RequestContext.HttpContext.Response.Write( string .Format( " Action({0})已经执行了!<br /> "
,filterContext.ActionDescriptor.ActionName));
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.Write( string .Format( " Action({0})执行之前!<br /> "
,filterContext.ActionDescriptor.ActionName));
}
2、 IResultFilter(Result拦截器接口)
IResultFilter 和 IActionFilter 一样提供2个方法,执行前和执行后,分别是在 返回Result之前执行和返回Result之后执行。接口中的2个方法如下:
// Called after an action result executes.
//
// 参数:
// filterContext:
// The filter context.
void OnResultExecuted(ResultExecutedContext filterContext);
//
// 摘要:
// Called before an action result executes.
//
// 参数:
// filterContext:
// The filter context.
void OnResultExecuting(ResultExecutingContext filterContext);
参数:filterContext包含拦截到的Result等信息。。。
示例代码如:
public void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.Write( " Result已经执行了! " );
}
public void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.Write( " Result执行之前! " );
}
#endregion
这2个接口的区别很明显:一个拦截 Action、一个拦截Result(这不是废话嘛,哈哈),又因为Result是Action返回的,
所以 他们的执行顺序是:
OnActionExecuting-->Action中的代码-->OnActionExecuted-->OnResultExecuting-->OnResultExecuted
由于拦截器的特性,大伙们现在喜欢用他们来做日志记录,判断权限等功能。。。。。
当然做日志记录还经常要记录一些异常信息了。。
3、IExceptionFilter(异常拦截器接口)
异常拦截器接口只有一个方法,就是当异常发生时需要执行的内容:
// Called when an exception occurs.
//
// 参数:
// filterContext:
// The filter context.
void OnException(ExceptionContext filterContext);
参数:filterContext包含异常等信息。。。
示例代码如:
public void OnException(ExceptionContext filterContext)
{
string controller = filterContext.RouteData.Values[ " controller " ] as string ;
string action = filterContext.RouteData.Values[ " action " ] as string ;
filterContext.RequestContext.HttpContext.Response.Write( string .Format( " {0}:{1}发生异常!{2} " ,
controller,action, filterContext.Exception.Message));
filterContext.ExceptionHandled = true ;
}
#endregion
代码中显示了出错的Controller 、action 和异常信息。。。。此处你可以自定义重定向到错误页面去。。
上面说到大伙们可能会用 IActionFilter做权限判断功能,其实我们可以用专业的 IAuthorizationFilter来做权限判断。。。
4、IAuthorizationFilter(授权拦截器接口)
该拦截器专门用来判断权限,判断时候有权限执行后面的Action,此接口在任何拦截器之前执行。。。。。
提供一个方法:
// Called when authorization is required.
//
// 参数:
// filterContext:
// The filter context.
void OnAuthorization(AuthorizationContext filterContext);
示例代码如:
public void OnAuthorization(AuthorizationContext filterContext)
{
filterContext.HttpContext.Response.Write( " 执行authorization! 判断是否有权限。。。。<br /> " );
}
#endregion
完整示例代码:
1、MyFilter.cs
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication3
{
public class MyFilter : FilterAttribute, IActionFilter, IResultFilter, IExceptionFilter, IAuthorizationFilter
{
#region IActionFilter 成员
public void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.Write( string .Format( " Action({0})已经执行了!<br /> "
,filterContext.ActionDescriptor.ActionName));
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.Write( string .Format( " Action({0})执行之前!<br /> "
,filterContext.ActionDescriptor.ActionName));
}
#endregion
#region IResultFilter 成员
public void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.Write( " Result已经执行了! " );
}
public void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.Write( " Result执行之前! " );
}
#endregion
#region IExceptionFilter 成员
public void OnException(ExceptionContext filterContext)
{
string controller = filterContext.RouteData.Values[ " controller " ] as string ;
string action = filterContext.RouteData.Values[ " action " ] as string ;
filterContext.RequestContext.HttpContext.Response.Write( string .Format( " {0}:{1}发生异常!{2} " ,
controller,action, filterContext.Exception.Message));
filterContext.ExceptionHandled = true ;
}
#endregion
#region IAuthorizationFilter 成员
public void OnAuthorization(AuthorizationContext filterContext)
{
filterContext.HttpContext.Response.Write( " 执行authorization! 判断时候有权限。。。。<br /> " );
}
#endregion
}
}
2、HomeController.cs
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication3;
namespace MvcApplication3.Controllers
{
[HandleError]
[MyFilter]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData[ " Message " ] = " Welcome to ASP.NET MVC! " ;
return View();
}
public ActionResult About()
{
Response.Write( " 执行about!<br /> " );
throw new Exception( " 我是假的异常,呵呵 " );
// return View();
}
}
}