WebAPI Controller Inheritance and Attribute Routing

I have several controllers that inherit from the same base class. Among the various activities that they do not share with each other, they have several that are completely identical. I would like to have them in my base class, because they all work in exactly the same way as they, they are accessible through different routes.

How to define these actions in several ways?

Inherited classes also have a RoutePrefixAttribute set, so each one points to a different route.

Example

I have a base abstract class called Vehicle , and then inherited Car , Bike , Bus , etc. All of them would have a common Move() action.

 /bus/move /car/move /bike/move 

How can I define the Move() action in my base Vehicle class so that it runs on every route of the subclass?

+8
source share
2 answers

Check the answer I gave here the attributes of the routing attributes of WebApi2 , which indicate the response from this message. NET WebAPI attribute Routing and inheritance

What you need to do is overwrite DefaultDirectRoutePrivider :

 public class WebApiCustomDirectRouteProvider : DefaultDirectRouteProvider { protected override IReadOnlyList<IDirectRouteFactory> GetActionRouteFactories(HttpActionDescriptor actionDescriptor) { // inherit route attributes decorated on base class controller actions return actionDescriptor.GetCustomAttributes<IDirectRouteFactory>(inherit: true); } } 

After that, you need to configure it in the web api configuration

 public static class WebApiConfig { public static void Register(HttpConfiguration config) { ..... // Attribute routing. (with inheritance) config.MapHttpAttributeRoutes(new WebApiCustomDirectRouteProvider()); .... } } 

Then you can do what you described as it

 public abstract class VehicleControllerBase : ApiController { [Route("move")] //All inheriting classes will now have a `{controller}/move` route public virtual HttpResponseMessage Move() { ... } } [RoutePrefix("car")] // will have a `car/move` route public class CarController : VehicleControllerBase { public virtual HttpResponseMessage CarSpecificAction() { ... } } [RoutePrefix("bike")] // will have a `bike/move` route public class BikeController : VehicleControllerBase { public virtual HttpResponseMessage BikeSpecificAction() { ... } } [RoutePrefix("bus")] // will have a `bus/move` route public class BusController : VehicleControllerBase { public virtual HttpResponseMessage BusSpecificAction() { ... } } 
+12
source

This is what I did, and it worked the way you mentioned in your question.

I created the base class ApiController and inherited from it all my API controllers. I defined the delete operation in my base class (which returns the string "Not Supported") and did not define delete on any of my child controllers. Now, when I do the deletion on any of my controller, I get the message "Not Supported", that is, the "Base Class" method is called. (I make a Delete request for Child, not for ie / Bike / move base)

But if I define Delete on any of the controllers, it gives me a warning about the implementation of the Hiding base, but when I execute the Delete request for api, I get - "An error has occurred."

I have not tried making RoutePrefix.

0
source

All Articles