Attribute routes should follow normal inheritance rules


This was apparently by design: "We considered this scenario but the problem in some scenarios users could be having very specific route templates to their actions in the base controller in which case inheriting this to the sub class is incorrect." [http://stackoverflow.com/questions/19989023/net-webapi-attribute-routing-and-inheritance]

The framework has functionality to deal with this already. Declare the base class with said "very specific route templates" sealed for starters. In my opinion this is intentionally violating LSP if one considers a route part of the public interface, which is always the case for a web api.

A compromise could be reached easily by also testing if the base class is concrete.
                // Ignore the Route attributes from inherited actions.
                if (action.MethodInfo != null &&
                    action.MethodInfo.DeclaringType != controller.ControllerType &&
                    factories = null;
Or something similar.
This would in general be a non-breaking change as one would not have added routes to an abstract base class without intending them to be inherited.

Closed Jan 7 at 9:05 PM by danroth27
Due to the complexities around defining semantics for the inheritance of attribute routes they are not currently supported.


timhardy wrote Feb 15 at 4:45 AM

I'd like further clarification as to the complexities. I'm not saying they don't exist, but I would like some clarification. Is it not possible to build a few simple rules...

If an action in the base class is not overridden in the derived class, name the route as <DerivedControllerName>.<BaseClassRouteName>, this gives unique names to every derived controller route.

For overridden methods...
If the derived class provides its own Route attribute, then the base Route is ignored/overridden.

Is the above not possible the way attribute routes are implemented? This just seems like a no-brainer to me, and a serious deficiency in Web Api 2.

timhardy wrote Feb 15 at 4:49 AM

As to complexity, it wasn't too complex for attributerouting.net - https://github.com/mccalltd/AttributeRouting/wiki/Inherit-Routes-from-Actions-in-Base-Controllers

I seriously don't understand why this decision was made. It was obviously made without consulting the community.