Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger JSON relative path

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?

like image 394
Malakadreas Avatar asked Nov 28 '22 22:11

Malakadreas


2 Answers

I was with this same problem Try this:

        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("v1/swagger.json", "V1 Docs");                
        });
like image 175
Luciano Azevedo Avatar answered Dec 12 '22 11:12

Luciano Azevedo


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

like image 35
ekim Avatar answered Dec 12 '22 13:12

ekim