What is AddEndpointsApiExplorer in ASP.NET Core 6.
What is AddEndpointsApiExplorer in ASP.NET Core 6.
1290
07-Nov-2023
Updated on 08-Nov-2023
Aryan Kumar
08-Nov-2023In ASP.NET Core 6, AddEndpointApiExplorer is an extension method that is part of the Microsoft.AspNetCore.Mvc.Versioning package. This method is used for configuring API versioning and API explorer (Swagger) in your ASP.NET Core Web API application.
API versioning is a technique that allows you to version your API endpoints, making it possible to introduce breaking changes or new features while maintaining backward compatibility with existing clients. API explorer, often implemented using Swagger, is a tool that generates interactive documentation for your APIs, making it easier for developers to understand and use your endpoints.
Here's how you can use AddEndpointApiExplorer in your ASP.NET Core 6 application:
Install Required NuGet Packages:
To use AddEndpointApiExplorer, you need to install the required NuGet packages:
Configure API Versioning and Explorer:
In your Startup.cs file, typically in the ConfigureServices method, configure API versioning and API explorer as follows:
In this code:
services.AddApiVersioning is used to configure API versioning. You can specify various options like reporting API versions, setting a default version, and more.
services.AddVersionedApiExplorer is used to configure API explorer. You can set options for grouping and substituting API versions in URLs.
Use API Explorer in Middleware:
In your Startup.cs file, typically in the Configure method, configure API explorer for generating Swagger documentation:
In this code, app.UseSwagger and app.UseSwaggerUI are used to enable the Swagger documentation and provide interactive API documentation based on the configured API versions.
Using AddEndpointApiExplorer in combination with API versioning and API explorer allows you to version your APIs and generate documentation to make them more discoverable and accessible for developers. It's a valuable tool when building and maintaining complex ASP.NET Core Web API applications with different versions.