Swagger JSON relative path
Asked Answered
C

2

10

In my ASP 4.6 application I have:

app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Feature Toggle API V1"); });

When I run it on localhost:5000/swagger/v1/swagger.json it works fine but when I deploy it on a remote server that uses virtual directories for different applications someserver/myapp/swagger/v1/swagger.json it doesn't work.

What is the best practice in turning app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Feature Toggle API V1"); });to a relative path that works on localhost as well as within a virtual directory?

Cataclysm answered 28/4, 2017 at 12:0 Comment(0)
T
34

I was with this same problem Try this:

        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("v1/swagger.json", "V1 Docs");                
        });
Thermonuclear answered 28/4, 2017 at 21:0 Comment(1)
This still works on .NET 6 and should be the accepted answer.Borderland
E
3

Maybe this question is a bit old, but I had the same problem and found the answer in the ASP.NET Core documentation:

If using directories with IIS or a reverse proxy, set the Swagger endpoint to a relative path using the ./ prefix. For example, ./swagger/v1/swagger.json. Using /swagger/v1/swagger.json instructs the app to look for the JSON file at the true root of the URL (plus the route prefix, if used). For example, use http://localhost://swagger/v1/swagger.json instead of http://localhost:///swagger/v1/swagger.json.

Original link: https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-2.2&tabs=visual-studio

Edik answered 24/6, 2019 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.