I have setup my ASP.NET core using the middleware UsePathBase("/api/something/else")
, and my controllers are defined like this:
[ApiController]
[Route("/[controller]")]
public class WeatherForecastController : ControllerBase
and the specific endpoint is defined like this
[HttpGet]
public async Task<IEnumerable<ForecastRecord>> Get()
When I run the application I can access the endpoint like this:
/WeatherForecast
/api/something/else/WeatherForecast
When I get the Swagger documentation I get this:
"servers": [
{
"url": "/api/something/else"
}
],
"paths": {
"/WeatherForecast": {
"get": {
"tags": [
"WeatherForecast"
],
As you can see the servers
element have a entry to /api/something/else
but when I run the SwaggerUI I don't see the endpoints prefixed with /api/something/else
:
I am wondering if there is a way to force the SwaggerUI to prefix the endpoints with the value set with UsePathBase
After playing here and there with the order of middleware calls, I have found the right combination to solve the problem described in this question is:
//1. Add SwaggerUI
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "api/something/else";
c.SwaggerEndpoint("swagger/v1/swagger.json", "Name");
});
//2. Set BasePath
app.UsePathBase("/api/something/else");
//3. Add Swagger
app.UseSwagger();
Following that order the Swagger UI will show properly the endpoints: