Skip to content

Instantly share code, notes, and snippets.

@ichiroku11
Created September 10, 2013 13:08
Show Gist options
  • Select an option

  • Save ichiroku11/6509158 to your computer and use it in GitHub Desktop.

Select an option

Save ichiroku11/6509158 to your computer and use it in GitHub Desktop.
FilterAttribute で FilterScope の違いによる実行順序を確認してみた
using System.Web;
using System.Web.Mvc;
namespace MvcApp {
public class FilterConfig {
public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
filters.Add(new SampleAttribute("Global"));
}
}
}
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
*/
}
}
@ichiroku11

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment