Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI controller inheritance and attribute routing

I have few controllers that inherit from the same base class. Among the different actions that they don't share with each other, they do have a few that are completely identical. I would like to have these on my base class because they all work completely the same it's just that they're accessed through different routes.

How should I define these actions with several different routes?

My inherited classes also have a RoutePrefixAttribute set on them so each of them is pointing to a different route.

Example

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

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

How can I define action Move() on my base class Vehicle so that it will be executed on each subclass route?

like image 688
Robert Koritnik Avatar asked Apr 28 '14 14:04

Robert Koritnik


People also ask

What is attribute routing in Web API?

Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.

What is the use of controller level attribute routing?

Controller level attribute routing You can define routes at controller level which apply to all actions within the controller unless a specific route is added to an action.

How many types of routing are there in Web API?

Web API supports two types of routing: Convention-based Routing. Attribute Routing.

What is the difference between Web API and MVC routing?

If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP verb, not the URI path, to select the action. You can also use MVC-style routing in Web API.


1 Answers

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 DefaultDirectRouteProvider:

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() {
        ...
    }
}
like image 171
Nkosi Avatar answered Oct 09 '22 10:10

Nkosi