Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing an inherited Attribute in .net

Tags:

I got a ASP.NET MVC controller like this

[Authorize]
public class ObjectController : Controller
{
     public ObjectController(IDataService dataService)
     {
         DataService = dataService;
     }
     public IDataService DataService { get;set;}
}

The Authorize attribute is defined as "Inherited=true" in the framework. So when i make the next controller:

public class DemoObjectController : ObjectController 
{
    public DemoObjectController(IDataService dataService)
        : base (dataService)
    {
        DataService = new DemoDataService(DataService);
    }
}

It gets the authorize attribute, but i don't want it here. I want the Demo Object controller to be available to everyone, cause it just uses fake data.

I guess I'll implement my own Authorize attribute that don't get inherited, for i can't find any way to remove the attribute from the inherited class.

like image 803
AndreasN Avatar asked Jul 10 '09 13:07

AndreasN


1 Answers

Since it is marked as inherited, there isn't much you can do in this case (since you don't control the code that is checking for the attribute via reflection). Implementing your own attribute seems the most practical option.

With MVC you can also often achieve the same functionality with overrides (the On* methods), which might be worth looking into.

like image 185
Marc Gravell Avatar answered Oct 11 '22 23:10

Marc Gravell