Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save frankgeerlings/4761566 to your computer and use it in GitHub Desktop.
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); }
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