Reputation: 588
I have implemented the aspnet core api versioning as described here
here is some example code
services.AddApiVersioning(
options =>
{
options.ReportApiVersions = true;
options.DefaultApiVersion = new ApiVersion(0,0);
options.AssumeDefaultVersionWhenUnspecified = true;
} );
services.AddVersionedApiExplorer(
options =>
{
// note: the specified format code will format the version as "'v'major[.minor][-status]"
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
} );
Example Controller:
[Route("api/v{version:apiVersion}/[controller]")]
[ApiVersion("1")]
public class BreadcrumbsController : BaseController
{
...code removed...
}
Everything works fine if I add the [ApiController]
attribute to the controllers, but if I remove it then the IApiVersionDescriptionProvider
returns no results. The VersionApiExplorer seems to find no controllers. Unfortunately adding the [ApiController]
attribute overrides some filters we have implemented so I do not want to add it to the controllers.
How can I get the Versioned API Explorer to discover the controllers without adding the ApiController Attribute?
Upvotes: 5
Views: 2594
Reputation: 400
In the current version (8.1.0) this can be achieved by using:
services.AddTransient<IApiControllerFilter, NoControllerFilter>();
See https://github.com/dotnet/aspnet-api-versioning/issues/1029 for details.
Upvotes: 0
Reputation: 588
Digging more, I found in the release documentation https://github.com/Microsoft/aspnet-api-versioning/releases
ApiVersioningOptions.UseApiBehavior is now true by default
- This might result in excluding your API controllers if [ApiController] has not been applied
- If your API controllers use convention-based routing, they will definitely be excluded because [ApiController] cannot be applied
After reading that I added the suggested UseApiBehavior = false (it defaulted to true which was a breaking change) and it works correctly now
services.AddApiVersioning(
options =>
{
options.ReportApiVersions = true;
options.DefaultApiVersion = new ApiVersion(0,0);
options.AssumeDefaultVersionWhenUnspecified = true;
options.UseApiBehavior = false;
} );
Hopefully this will help someone else in the future
Upvotes: 8