The MVC controller gets the action name and controller name:
public class AuthorizeController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
string actionName = filterContext.ActionDescriptor.ActionName;
string controllerNamespace = filterContext.ActionDescriptor.ControllerDescriptor.ControllerType.FullName;
base.OnActionExecuting(filterContext);
}
}
Pretty straight forward.
But when I have ApiController ( System.Web.Http.ApiController), things get more complicated:

In the end, with some rsharper tips, I was able to reduce it to a few lines.
private string GetActionName(HttpControllerContext context)
{
var httpRouteDataCollection = context.RouteData.Values.Values;
var httpRouteDataCollection2 = httpRouteDataCollection.FirstOrDefault();
if (!(httpRouteDataCollection2 is IHttpRouteData[] httpRouteData))
{
return null;
}
IHttpRouteData routeData = httpRouteData.FirstOrDefault();
var httpActionDescriptorCollection = routeData?.Route.DataTokens["actions"];
if (!(httpActionDescriptorCollection is HttpActionDescriptor[] httpActionDescriptor))
{
return null;
}
HttpActionDescriptor reflectedHttpActionDescriptor = httpActionDescriptor.FirstOrDefault();
return reflectedHttpActionDescriptor?.ActionName;
}
Is it easier to do this? The reason for this is because I am currently implementing a general way of determining who can discover which action. Some actions are within WebApi and every time I need to execute the above “request”. Thus, all this turns things into a while.
WHY?
, , 40 MVC- 20 API 5-10 . ( ) Identity. , . , , .