3

I have a project with an ASP.net Web API. Now i have created a custom attribute. I can set that attribute for each controller, but how can i set it on each controller at once? Is there something for in the startup class?

2 Answers 2

3

Go to the App_Start > WebApiConfig.cs file.

In the Register method add this line:

config.Filters.Add(new MyCustomAttribute());
Sign up to request clarification or add additional context in comments.

Comments

1

1. You can use a base controller and use it as a base class on every of your other controllers.

    [YourCustomAttribute]
    public class BaseController : ApiController 
    {
    }

    public class HomeController : BaseController 
    {
    }

2. Another solution is to use Conditional Filters in ASP.NET MVC 3

Using this solution you can add the attribute without a base class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.