Our product is a client/server
app that has multiple versions of the client out in the field but has only one server that runs the latest version to service all API
calls. We have/will have hundreds of API
endpoints and I'm trying how best to handle versioning. What I'd like to do is be able to avoid the laborious task of applying attributes
to every single method, or copy entire controllers every time we make a minor change.
I might be misinterpreting most documents/practices on this, but it seems like every time you bump your API
you have to go through and do all that work, which seems inefficient at best.
Instead what I'd like to do is apply an attribute
to each endpoint with the version of when it was written, then the client finds the the closest version that is equal to or less than the client version.
For instance, if an endpoint was written at [ApiVersion("1.0")]
then that is the attribute it gets. If we had to modify it, I'd copy the method, rename it, apply a RoutePrefix
attribute
so it gets properly hit and apply a new attribute
with the version of our whole API
(in this example I put 1.5
).
Here is a simple example:
[HttpGet]
[ApiVersion("1.0")]
[Route("GetHeartBeat")]
public bool GetHeartBeat()
{
return true;
}
[HttpGet]
[ApiVersion("1.5")]
[Route("GetHeartBeat")]
public bool GetHeartBeat2()
{
return false;
}
This works no problem when I use url versioning:
/api/v1.0/GetHeartBeat
or/api/v1.5/GetHeartBeat
but /api/v1.3/GetHeartBeat
does not since that version doesn't exist..
What I want to happen is if I have a client that is running 1.3
, then it will find the closest version that is equal to or less than the latest version. So /api/v1.3/GetHeartBeat
would get received, since 1.3
doesn't exist, then it'll look at the closest/earlier version, in this case would be 1.0
.
I can write a bunch of route logic to accomplish this, but I feel like there has to be a solution out of the box as I can't be the first person to try this. Is there a nuget
package that would accomplish this?