Hossein
Hossein

Reputation: 1090

How to tell if an attribute call is originated from Controller vs. Action

When an action filter is called, is there any way to determine whether this call is originated from an attribute applied at controller level or action level?

I need myAttribute to be run for all of my action methods. The Delete action method, however, is specifically annotated with the filter so myAttribute is called twice. I need the call originated from Controller to do nothing or potentially do different things in that case.

Is there anyway to do this without actually removing the [myAttribute] from controller?

[myAttribute]
public class HomeController
{
    public ViewResult Index()
    {
    }

    public ViewResult View()
    {
    }

    public ViewResult Edit()
    {
    }

    [myAttribute]
    public ViewResult Delete()
    {

    }
}

I am using these action filters to authorize a user. A user could have access to a controller but if a method specifically demands permission (by having [myAttribute] above it), then controller level access is not enough and that action should be explicitly mentioned in user permissions.

Upvotes: 2

Views: 241

Answers (1)

crypted
crypted

Reputation: 10306

Not a solution, What you need to understand is Attributes is designed to use for Annotation only not to define Behavior. Moreover, When you apply Filter attribute on a Controller, It actually meant to apply for all the ActionMethods within the controller.

What I suggest is, Create one more FilterAttribute that have specific work related to your Delete method and decorate your method with it.

Upvotes: 2

Related Questions