dotnet / aspnet-api-versioning

Provides a set of libraries which add service API versioning to ASP.NET Web API, OData with ASP.NET Web API, and ASP.NET Core.
MIT License
3.03k stars 703 forks source link

AddVersionedApiExplorer not working in Asp.Versioning #1061

Closed julianalsemmani closed 9 months ago

julianalsemmani commented 9 months ago

Hey, so I just switched to Asp.Versioning, but can't seem to get this working. Everything else works fine.

services.AddVersionedApiExplorer(options =>
{
    options.GroupNameFormat = "'v'VVV";
    options.SubstituteApiVersionInUrl = true;
});

Any solution for this one? Tried searching a bit round, but couldn't find anything specific.

commonsensesoftware commented 9 months ago

Is this for ASP.NET Core or ASP.NET Web API? It's hard to tell. It looks like it might be ASP.NET Core due to the services variable name, so I'll roll with that assumption.

Starting in 6.0, all of the API Versioning features hang off of IApiVersioningBuilder. This centralizes configuration in a consistent way. The change is documented in the migration guide.

It's not clear if you only have Minimal APIs or you also have controllers. Regardless, the configuration is nearly identical:

var builder = WebApplication.CreateBuilder( args );
var services = builder.Services;

services.AddApiVersioning()     // Core API Versioning services with support for Minimal APIs
        .AddMvc()               // API version-aware extensions for MVC Core with controllers (not full MVC)
        .AddApiExplorer()       // API version-aware API Explorer extensions

The API Explorer extensions are included in Asp.Versioning.Mvc.ApiExplorer. The old extension method was AddVersionedApiExplorer because it would have otherwise been ambiguous with the built-in AddApiExplorer. Now that the extension methods all hang off of IApiVersioningBuilder, the names can be more succinct and are unambiguous.

julianalsemmani commented 9 months ago

Sorry for not being more specific. It's for ASP.NET Core Web API.

commonsensesoftware commented 9 months ago

No worries. I figured as much. ASP.NET Web API runs on the ASP.NET (4.x) Classic stack using the .NET Framework. You can, however, build a web API on ASP.NET Core, which runs on .NET. They are both ASP.NET and you can build web APIs with them both. API Versioning supports them both. 90%+ of the time people mean ASP.NET Core, but every once in a while, a question or issue comes up about the old stack, so I like to clarify. Thanks MS Marketing for making things unambiguously easy to understand! 😵‍💫

Hopefully, that solution works for you.

julianalsemmani commented 9 months ago

I got one more question. In the Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer I was using "IApiVersionDescriptionProvider". Is there any alternatives for this in Asp.Versioning?

I tried installing Asp.Versioning.WebApi, but do I need Asp.Versioning.Mvc as well?

julianalsemmani commented 9 months ago

Solved this! Figured out that I needed to use Asp.Versioning.Mvc and not the Asp.Versioning.WebApi!

@commonsensesoftware Thanks for helping out with the other stuff! I'll close this issue! 😄