Created
March 11, 2011 11:54
-
-
Save beccasaurus/865800 to your computer and use it in GitHub Desktop.
Rails-like before filter
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
| public class BeforeAttribute : ActionFilterAttribute { | |
| public BeforeAttribute() { | |
| Methods = string.Empty; | |
| Except = string.Empty; | |
| Only = string.Empty; | |
| } | |
| public BeforeAttribute(params string[] methodNames) : this() { | |
| Methods = string.Join(" ", methodNames); | |
| } | |
| public virtual string Methods { get; set; } | |
| public virtual string Except { get; set; } | |
| public virtual string Only { get; set; } | |
| public virtual string[] MethodNames { get { return Methods.Split(' '); } } | |
| public virtual string[] ExceptActions { get { return Except.Split(' '); } } | |
| public virtual string[] OnlyActions { get { return Only.Split(' '); } } | |
| // This is only called if we check Except/Only actions and determine that we should get a result | |
| public ActionResult GetResult(ActionExecutingContext filterContext) { | |
| ActionResult result = null; | |
| var type = filterContext.Controller.GetType(); | |
| foreach (var methodName in MethodNames) { | |
| var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); | |
| result = method.Invoke(filterContext.Controller, new object[]{}) as ActionResult; | |
| if (result != null) | |
| break; | |
| } | |
| return result; | |
| } | |
| public override void OnActionExecuting(ActionExecutingContext filterContext) { | |
| var actionName = filterContext.ActionDescriptor.ActionName; | |
| ActionResult result = null; | |
| if (OnlyActions.Any()) | |
| if (OnlyActions.Contains(actionName)) | |
| result = GetResult(filterContext); | |
| else | |
| if (! ExceptActions.Contains(actionName)) | |
| result = GetResult(filterContext); | |
| if (result == null) | |
| base.OnActionExecuting(filterContext); | |
| else | |
| filterContext.Result = result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment