I'm upgrading an ASP.NET Core API project from v5 to v6.
Service config in v5:
services.AddSwaggerGen();
Service config in v6:
builder.Services.AddEndpointsApiExplorer(); // what is this?
builder.Services.AddSwaggerGen();
What is AddEndpointsApiExplorer? Everything works as expected whether I add it or not.
I use the "ASP.NET API Versioning" library. Are they related? If so, must I use both, just the library, or is the library unnecessary now?
AddEndpointsApiExploreris for Minimal APIs whereasAddApiExplorerrequires, at least, MVC Core. For API projects,AddControllerscallsAddApiExploreron your behalf.But Why Does Everything Still Work With
AddEndpointsApiExplorer?With the introduction of Endpoint Routing, everything in the routing system boils down to an
Endpoint. ASP.NET Core uses the Application Model, namelyApplicationModel,ControllerModelandActionModelto createEndpointinstances and register them with the routing system. Minimal APIs, however, use a builder to directly create and register individualEndpointinstances.The default API Explorer implementation provides a
IApiDescriptionProviderthat buildsApiDescriptioninstances from the Application Model. Minimal APIs do not have an Application Model so there is nothing to buildApiDescriptioninstances from. The API Explorer provides these descriptions, which are commonly used by tools such as OpenAPI generators. Without any descriptions, there would be no support for Minimal APIs and OpenAPI; that would be bad (or, at least, certainly not accepted by developers). To address that, the ASP.NET Core team created a secondIApiDescriptionProviderthat only considersEndpoint.If Everything is an
Endpoint, Why Not Merge Implementations?There're two parts to this answer. First, changing the original
IApiDescriptionProviderimplementation would introduce a public, breaking change. At a minimum, new constructor arguments would be required. Since it was a major version bump, this approach wasn't off the table, but it turns out to be irrelevant. The bigger issue is that the originalIApiDescriptionProviderimplementation andAddApiExplorerlive in and depend on MVC Core. Minimal APIs only require the routing abstractions. There is no way to merge the two without adding unnecessary coupling. To address this problem,AddEndpointsApiExplorerwas added which adds an implementation that only requires anIApiDescriptionProviderimplementation based on bare bonesEndpointdefinitions from the routing system.If
AddEndpointsApiExplorerexists and I call it, do I even needAddApiExploreranymore? Maybe. The metadata exposed and available on Minimal APIEndpointinstances is much lighter than the Application Model; after all, they are minimal. Behind the scenes, aIApiDescriptionGroupCollectionProviderimplementation takes a sequence ofIApiDescriptionProviderinstances. IfAddEndpointsApiExplorerandAddApiExplorerare called, then both providers will execute. If onlyAddEndpointsApiExploreris called, it will work with regular 'ol controllers, but the descriptions' information fidelity might be less than what you are accustomed to. If you are only authoring Minimal APIs, thenAddEndpointsApiExploreris required if you want API Explorer support.