Swagger UI "Try It Out" option for DotNet Core 6 API app does not work with nginx ingress in azure kubernetes service (aks) , the path for service does not get added in the URL . I have tried multiple options like adding swagger endpoint in program.cs or removing the target-rewrite option but nothing worked.

The swagger UI works fine with localhost and with LoadBalancer service but not with nginx controller as shown below:

Below is my Ingress configuration

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hello-world-ingress
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /serv1/(.*)
        pathType: Prefix
        backend:
          service:
            name: myapp
            port:
              number: 80

Below is how my "program.cs" file looks

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();
//     options =>
//     options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
//     //options.RoutePrefix = "serv1/swagger";
// });
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
											

Hi, I just add some clarification as I have the same issue but I understand from answers that the context is not clear.

Let's suppose you have two services (web API) with two ingresses and one host name.
Your APIs respond to a GetInfo url

You set up your ingresses to rewrite url using two prefixes:
-)/myhostname/prefix1/GetInfo --> /service1/GetInfo
-)/myhostname/prefix2/GetInfo --> /service2/GetInfo

When you call your APIs from any Postman/browser/client you have no issue.
When you open swagger ui and press Try it now, you actually make a request to /myhostname/GetInfo instead of /myhostname/prefix1/GetInfo and you got your 404

The LB nginx ingress provisions sets its heath check to look at the / path, and the dotnet api doesnt respond at all on /! You can change the path when you install the controller as described here, that should ping it to life: https://github.com/Azure/AKS-Construction/issues/573#issuecomment-1527253720