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 702 forks source link

Different options in `ApiVersioningOptions.cs` between .NET Framework and .NET Core packages #1035

Closed Saibamen closed 10 months ago

Saibamen commented 10 months ago

Is there an existing issue for this?

Describe the bug

We fixed one bug in our Legacy (but still in active development) .NET Framework WebAPI by setting ControllerNameConvention = new OriginalControllerNameConvention();.

Now I want to do the same in .NET Core WebAPI, but IControllerNameConvention is missing in ApiVersioningOptions.cs...

.NET Framework NuGet name and version: Microsoft.AspNet.WebApi.Versioning 5.1.0 .NET Core NuGet name and version: Asp.Versioning.Mvc.ApiExplorer 7.1.0

image

Expected Behavior

.NET Core should have all options from official documentation. Controller name should be PrintV2 in Swagger in .NET Core.

image

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

8.0.100-rc.2.23502.2

Anything else?

.NET Core 7.0.13

commonsensesoftware commented 10 months ago

This option still exists, but it is now only set through dependency injection (DI). Web API doesn't have out-of-the-box DI support so the option needs to be explicit in the options. In ASP.NET Core, Minimal APIs and MVC Core are separated by the AddApiVersioning() and AddMvc() respectively. I considered moving ControllerNameConvention over to the MVC Core options, but I really wanted to reduce the number of configurable options which are better suited for DI. In fact, the naming convention could always be set via DI. Setting it via the options was a secondary way.

You can find the source at:

https://github.com/dotnet/aspnet-api-versioning/blob/471eb044500495beb8a4f009d9a1c2bf4d5fde63/src/AspNetCore/WebApi/src/Asp.Versioning.Mvc/DependencyInjection/IApiVersioningBuilderExtensions.cs#L66

To achieve your desired configuration, you need only set or replace the service you want to use:

builder.Services.AddSingleton<IControllerNameConvention, OriginalControllerNameConvention>();

In fairness, I missed adding this change to the migration guide. I have updated the section on configuration to note this difference.

I hope that helps.

Saibamen commented 10 months ago

Thank you. It works :)