Search code examples
c#.netasp.net-mvc-4authorization

How to know which controller method will be called from Web API Authorization filter


I have a custom AuthorizationFilter class to handle authorization to my API. Now, I need to enhance it by adding some attributes to methods which will be read only in some situations.

I can get the controller from actionContext.ControllerContext but:

How can I know which Controller method will be called from the IsAuthorized method of my custom AuthorizeAttribute class? So I can get it's attributes with reflection.

Edit: Adding more info-

If I get a call like localhost/api/myapi/?id=4 I want to get the real name of the method that will be executed in the controller like GetById(int id).

That way I could check if the method has any custom attributes I need added to it.


Solution

  • In web api 2 you can access the action name with:

    actionContext.ActionDescriptor.ActionName