WebAPI controller inheritance and attribute routing

点点圈 提交于 2019-11-28 01:03:55
Nkosi

Check the answer I gave here WebApi2 attribute routing inherited controllers, which references the answer from this post .NET WebAPI Attribute Routing and inheritance

What you need to do is overwrite the DefaultDirectRoutePrivider:

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

With that done you then need to configure it in your web api configuration

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

You will then be able to do what you described like this

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() {
        ...
    }
}

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

I created base ApiController class and inherited all my API controllers from it. I defined Delete operation in my Base class (which returns string "Not Supported") and didn't define delete on any of my child controller. Now when I do a delete on any of my controller I get the message "Not Supported" i.e. Base class's delete is called. ( I am doing Delete request on Child, and not on base i.e. /Bike/move)

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

I haven't tried doing RoutePrefix way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!