Created
February 12, 2013 10:58
-
-
Save frankgeerlings/4761566 to your computer and use it in GitHub Desktop.
Allows you to check what the current action is on action filters, e.g:
if (filterContext.ActionDescriptor.DescribesAction<AccountController>(a => a.Login())) { Do(stuff); }
This file contains 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.Linq.Expressions; | |
using System.Web.Mvc; | |
public static class ActionDescriptorDescribesActionExtension | |
{ | |
public static bool DescribesAction<TController>(this ActionDescriptor self, Expression<Func<TController, object>> action) where TController : IController | |
{ | |
var actionName = | |
(action.Body is UnaryExpression | |
? (MethodCallExpression)((UnaryExpression)action.Body).Operand | |
: (MethodCallExpression)action.Body).Method.Name; | |
var controller = typeof(TController); | |
return actionName == self.ActionName && controller == self.ControllerDescriptor.ControllerType; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment