RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.78k stars 1.29k forks source link

Serve Swagger UI on path / with global.asax #1074

Closed thiagobucca closed 6 years ago

thiagobucca commented 6 years ago

Hello,

Is there any way to load swagger without having to type /swagger? I mean just start the solution and go to swagger directly as the initial page.

I'm on Visual Studio 2017 and using NSwag OWIN middleware in global.asax

Thanks in advance

RicoSuter commented 6 years ago

You can change the url with the SwaggerUiRoute setting:

https://github.com/RSuter/NSwag/blob/master/src/NSwag.AspNetCore/SwaggerUiSettingsBase.cs#L19

thiagobucca commented 6 years ago

But that parameter doesn't take "/" for an option, right?

Let's say my start url in Visual Studio is: http://localhost:54000, what i have to set in the SwaggerUiRoute to match my start url?

RicoSuter commented 6 years ago

/ would be the correct owin path for that i think.. have you tried it?

thiagobucca commented 6 years ago

Yes. When i set / for the path swagger loads with "Can't read from server image". That doesn't happen when my owin path is "swagger"

Here's my global.asax

`RouteTable.Routes.MapOwinPath("/", app => { app.UseSwaggerUi(typeof(WebApiApplication).Assembly, new SwaggerUiOwinSettings {

                DocumentProcessors =
                {
                    new SecurityDefinitionAppender("apikey", new SwaggerSecurityScheme
                {
                   Type = SwaggerSecuritySchemeType.ApiKey,
                    Name = "token",
                   In = SwaggerSecurityApiKeyLocation.Header
                })

                },
                MiddlewareBasePath = "/swagger",
            });
        });`
RicoSuter commented 6 years ago

Maybe you also have to change the MiddlewareBasePath to /?

thiagobucca commented 6 years ago

Yes, unfortunately that was my first try, i get the following error:

The path must start with a '/' followed by one or more characters. Parameter name: value

That message makes me wonder that MiddlewareBasePath doesn't take / for an option.

RicoSuter commented 6 years ago

I have to try this with the sample project. All paths must be correct in order to make this work with owin. Cant you just simply redirect from / to /swagger?

heldersepu commented 6 years ago

Why not just created a redirect file, I have something like that here: http://offleaseonly.azurewebsites.net/ Is there a specific need to serve the swagger-ui on root?

thiagobucca commented 6 years ago

No specific need, just to when we start the solution in Visual Studio, directly go to /swagger without have to type. Can i have this with a redirect file?

heldersepu commented 6 years ago

Sure you can, that's what I did...

heldersepu commented 6 years ago

Here is my fancy redirect: https://github.com/heldersepu/OffLeaseOnly/blob/master/OffLeaseOnly/index.html

thiagobucca commented 6 years ago

Nice! Thank you for your help.

heldersepu commented 6 years ago

... for the record my redirect is just workaround! if someone still need to get the swagger-ui on root (not sure why anyone will need that) that issue still unsolved

thiagobucca commented 6 years ago

Yes, i agree. I only closed because i don't know if this is really a issue, i'll let @RSuter decide.

RicoSuter commented 6 years ago

I personally dont use global.asax based projects anymore. If this is needed for you and the “hacks” (redirect, or set startup path to /swagger) are not enough, you can try to fix it in the nswag sample project.

StephanMoeller commented 4 years ago

I too have the wish of serving wagger at the root. Having api.mydomain.com showing the documentation directly seems clean. Seems a little silly this is not an option and weird that the team does not recognize this as a need.