-
Notifications
You must be signed in to change notification settings - Fork 714
-
For the moment, we're using the following syntax to define which versions can be used by a controller.
[ApiController] [ApiVersion(ApiVersions.V1_0)] [ApiVersion(ApiVersions.V2_0)] public class MyController : ControllerBase { }
But when a new version is added, how is it possible to make it accessible to all controllers without having to add a new [ApiVersion(ApiVersions.V3_0)]
attribute to the top of all controllers?
Is it possible to indicate the version from which a controller is available?
Examples
[ApiVersionRange(From: ApiVersions.V1_0)]
[ApiVersionRange(From: ApiVersions.V1_0, To: ApiVersions.V3_0)]
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Out-of-the-box, the short answer is - no; however, there are many extensions to enable it. This type of solution is very specific to your application and cannot be easily genericized. API versions must be explicit, which is intrinsically why it isn't supported by default. The step is one of the biggest challenges. What should the step between versions be? A major version? A minor version? How about if the version is a date?
There are several possible solutions, but there is no universal answer that suites everyone (or it would be baked in). There are a number of options presented in discussions and (closed) issues.
At a high level, you have several options:
- Extend
ApiVersionAttribute
orApiVersionsBaseAttribute
- Roll your own attributes; API Versioning only cares about
IApiVersionProvider
- Use conventions
- These can be convention only
- These can be a convention using your own custom attributes
- These can be explicitly configured via options
If all of your controllers are symmetrical and use the same set of versions, a custom convention (a la IControllerConvention
) will have the least amount of churn as it will apply to all controllers. I suspect you'll find what you're looking for in the archives, but if not, I'm happy to help you flush out a more concrete solution.
Sidebar: The same is true for
[ApiController]
. You can apply[assembly: ApiController]
and all controllers in the assembly will be considered API controllers so that you don't have to apply the attribute to every single controller. It also avoids the scenario where you forget to apply it.
Beta Was this translation helpful? Give feedback.