I have two kubernetes services deployed on a AKS, they receive traffic from a Nginx Ingress Controller. The endpoints for these two services are https:<dns>/service1
and https:<dns>/service2
. Now I want to set up Swagger for each services. Below is how I set up Swagger UI for one of the services.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/service1/swagger/v1/swagger.json", "API V1");
});
With this configuration, I can get access to swagger by https:<dns>/service1/swagger
.
Now the problem is, in Swagger UI, when I want to test the api by clicking the "Try it out" button then Excute button, the url that Swagger UI access is https:<dns>/api/v1/contoller
instead of https:<dns>/service1/api/v1/contoller
. Which means that Swagger UI is not aware of the existance of path /service1/
. I found several related questions like this one How to change base url of Swagger in ASP.NET core
. But they are not the solution for my problem. My guess is I need to set a base path for Swagger. If anyone could tell me how to configure base path for Swagger in ASP.NET core 2.0, it would be much appreciated.
Change this:
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/service1/swagger/v1/swagger.json", "API V1");
});
to this:
For dotnet core 2.x
app.UseSwagger(c =>
{
#if !DEBUG
c.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.BasePath = "/service1");
#endif
});
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("./swagger/v1/swagger.json", "API V1");
});
For dotnet core 3.x (Swashbuckle 5.x prerelease+)
app.UseSwagger(c =>
{
#if !DEBUG
c.RouteTemplate = "swagger/{documentName}/swagger.json";
c.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Servers = new System.Collections.Generic.List<OpenApiServer>
{
new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}/service1" }
});
#endif
});
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("./swagger/v1/swagger.json", "API V1");
});
#if !DEBUG ... #endif is necessary for accessing the swagger ui while debugging in local machine.
Note: I'm assuming "/service1" is the same value as in your values.yaml file of your helm chart. (see below)
...
ingress:
enabled: true
annotations: {
kubernetes.io/ingress.class: "nginx",
nginx.ingress.kubernetes.io/rewrite-target: /$1
}
path: /service1/?(.*)
hosts:
- your-aks-subdomain.your-azure-region.cloudapp.azure.com
tls: []
# - secretName: chart-example-tls
# hosts:
# - chart-example.local
hpa:
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With