Created
September 10, 2013 13:08
-
-
Save ichiroku11/6509158 to your computer and use it in GitHub Desktop.
FilterAttribute で FilterScope の違いによる実行順序を確認してみた
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.Web; | |
| using System.Web.Mvc; | |
| namespace MvcApp { | |
| public class FilterConfig { | |
| public static void RegisterGlobalFilters(GlobalFilterCollection filters) { | |
| filters.Add(new SampleAttribute("Global")); | |
| } | |
| } | |
| } |
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 { | |
| [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] | |
| public class SampleAttribute : FilterAttribute, IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter { | |
| private readonly string _scope; | |
| public SampleAttribute(string scope) { | |
| _scope = scope; | |
| } | |
| private void Log() { | |
| Trace.WriteLine(string.Format("{0}:{1}", new StackTrace().GetFrame(1).GetMethod().Name, _scope)); | |
| } | |
| 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(); | |
| } | |
| } | |
| } | |
| namespace MvcApp.Controllers { | |
| [Sample("Controller")] | |
| public class SampleController : Controller { | |
| private void Log() { | |
| Trace.WriteLine(string.Format("{0}:First", 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(); | |
| } | |
| [Sample("Action")] | |
| public ActionResult Index() { | |
| return new EmptyResult(); | |
| } | |
| /* | |
| /sample/index | |
| Order が同じ場合は、FilterScope の順で実行される | |
| OnAuthorization:First | |
| OnAuthorization:Global | |
| OnAuthorization:Controller | |
| OnAuthorization:Action | |
| OnActionExecuting:First | |
| OnActionExecuting:Global | |
| OnActionExecuting:Controller | |
| OnActionExecuting:Action | |
| OnActionExecuted:Action | |
| OnActionExecuted:Controller | |
| OnActionExecuted:Global | |
| OnActionExecuted:First | |
| OnResultExecuting:First | |
| OnResultExecuting:Global | |
| OnResultExecuting:Controller | |
| OnResultExecuting:Action | |
| OnResultExecuted:Action | |
| OnResultExecuted:Controller | |
| OnResultExecuted:Global | |
| OnResultExecuted:First | |
| */ | |
| [Sample("Action")] | |
| public ActionResult Error() { | |
| throw new Exception("Test"); | |
| } | |
| /* | |
| /sample/error | |
| OnAuthorization:First | |
| OnAuthorization:Global | |
| OnAuthorization:Controller | |
| OnAuthorization:Action | |
| OnActionExecuting:First | |
| OnActionExecuting:Global | |
| OnActionExecuting:Controller | |
| OnActionExecuting:Action | |
| 型 'System.Exception' の初回例外が MvcApp.dll で発生しました | |
| OnActionExecuted:Action | |
| OnActionExecuted:Controller | |
| OnActionExecuted:Global | |
| OnActionExecuted:First | |
| OnException:Action | |
| OnException:Controller | |
| OnException:Global | |
| OnException:First | |
| */ | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ASP.NET MVC でのフィルタリング