I want a base controller that has a shared minimum for all my controllers, and then inherit each controller from this base controller, extending with whatever actions the specific controller is supposed to handle.
[RoutePrefix("api/myapi/{apiVersion}/{id}")]
public abstract class BaseController : ApiController
{
}
[RoutePrefix("foo")]
public class FooController : BaseController
{
// Supposed to have /api/myapi/{apiVersion}/{id}/foo base path
public void Get(int id)
{
// Expected: /api/myapi/v1.0/"some id"/foo
return Ok();
// Result: /foo
}
}
[RoutePrefix("bar")]
public class BarController : BaseController
{
// Supposed to have /api/myapi/{apiVersion}/{id}/bar base path
[Route("{action}", Name="BarAction")]
public void Get(int id)
{
// Expected: /api/myapi/v1.0/"some id"/bar/"an action"
return Ok();
// Result: /bar/{action}
}
}
So the idea is to have a base url like: http://hostname/api/myapi/v1.0/100 where 100 is just a random id.
Each controller that inherit from this base, should then extend the url, so FooController.cs should extend to GET http://hostname/api/myapi/v1.0/100/foo, while my BarController was expected to have http://hostname/api/myapi/v1.0/100/bar/"an action"
Since I'm using SwashBuckle to generate SwaggerUI for me, I have added a verion constraint into mig startup config:
Startup.cs:
var constraintResolver = new DefaultInlineConstraintResolver()
{
ConstraintMap =
{
["apiVersion"] = typeof(ApiVersionRouteConstraint)
}
};
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes(constraintResolver);
I'm running Owin self host version 5.3.x / WebApi 2.2.
Can anyone please tell me if it is possible to use and extend atribute prefixes from a base class, and how?