Created
September 10, 2013 12:55
-
-
Save ichiroku11/6509004 to your computer and use it in GitHub Desktop.
FilterAttribute の Order プロパティを指定した場合の実行順序を確認してみた
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Diagnostics; | |
| using System.Linq; | |
| using System.Web; | |
| using System.Web.Mvc; | |
| namespace MvcApp.Controllers { | |
| [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] | |
| public class SampleAttribute : FilterAttribute, IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter { | |
| private void Log() { | |
| Trace.WriteLine(string.Format("{0}:{1}", new StackTrace().GetFrame(1).GetMethod().Name, Order)); | |
| } | |
| public void OnAuthorization(AuthorizationContext filterContext) { | |
| Log(); | |
| } | |
| public void OnActionExecuting(ActionExecutingContext filterContext) { | |
| Log(); | |
| } | |
| public void OnActionExecuted(ActionExecutedContext filterContext) { | |
| Log(); | |
| } | |
| public void OnResultExecuting(ResultExecutingContext filterContext) { | |
| Log(); | |
| } | |
| public void OnResultExecuted(ResultExecutedContext filterContext) { | |
| Log(); | |
| } | |
| public void OnException(ExceptionContext filterContext) { | |
| Log(); | |
| } | |
| } | |
| [Sample, Sample(Order = 1), Sample(Order = 2)] | |
| public class SampleController : Controller { | |
| private void Log() { | |
| Trace.WriteLine(string.Format("{0}:Controller", new StackTrace().GetFrame(1).GetMethod().Name)); | |
| } | |
| protected override void OnAuthorization(AuthorizationContext filterContext) { | |
| Log(); | |
| } | |
| protected override void OnActionExecuting(ActionExecutingContext filterContext) { | |
| Log(); | |
| } | |
| protected override void OnActionExecuted(ActionExecutedContext filterContext) { | |
| Log(); | |
| } | |
| protected override void OnResultExecuting(ResultExecutingContext filterContext) { | |
| Log(); | |
| } | |
| protected override void OnResultExecuted(ResultExecutedContext filterContext) { | |
| Log(); | |
| } | |
| protected override void OnException(ExceptionContext filterContext) { | |
| Log(); | |
| } | |
| public ActionResult Index() { | |
| return new EmptyResult(); | |
| } | |
| /* | |
| /sample/index | |
| OnAuthorization、OnActionExecuting、OnResultExecuting は Order の昇順 | |
| Controller のメソッドは一番最初 | |
| OnActionExecuted と OnResultExecuted は逆順 | |
| OnAuthorization:Controller | |
| OnAuthorization:-1 | |
| OnAuthorization:1 | |
| OnAuthorization:2 | |
| OnActionExecuting:Controller | |
| OnActionExecuting:-1 | |
| OnActionExecuting:1 | |
| OnActionExecuting:2 | |
| OnActionExecuted:2 | |
| OnActionExecuted:1 | |
| OnActionExecuted:-1 | |
| OnActionExecuted:Controller | |
| OnResultExecuting:Controller | |
| OnResultExecuting:-1 | |
| OnResultExecuting:1 | |
| OnResultExecuting:2 | |
| OnResultExecuted:2 | |
| OnResultExecuted:1 | |
| OnResultExecuted:-1 | |
| OnResultExecuted:Controller | |
| */ | |
| public ActionResult Error() { | |
| throw new Exception("Test"); | |
| } | |
| /* | |
| /sample/error | |
| OnException は逆順 | |
| OnActionExecuted は例外が発生しても呼ばれる様子 | |
| OnAuthorization:Controller | |
| OnAuthorization:-1 | |
| OnAuthorization:1 | |
| OnAuthorization:2 | |
| OnActionExecuting:Controller | |
| OnActionExecuting:-1 | |
| OnActionExecuting:1 | |
| OnActionExecuting:2 | |
| 型 'System.Exception' の初回例外が MvcApp.dll で発生しました | |
| OnActionExecuted:2 | |
| OnActionExecuted:1 | |
| OnActionExecuted:-1 | |
| OnActionExecuted:Controller | |
| OnException:2 | |
| OnException:1 | |
| OnException:-1 | |
| OnException:Controller | |
| */ | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ASP.NET MVC でのフィルタリング