Nswag: Adding SwaggerRoutes "hijacks" site root ASP.NET (non core) WebApi

Created on 5 Dec 2018  路  6Comments  路  Source: RicoSuter/NSwag

By "hijacks" I mean that the site root returns swagger.json - file, instead of hitting a default controller.

Steps to reproduce
Using
ASP.NET, non core, webapi with OWIN.
app.UseSwagger(typeof(Startup).GetTypeInfo().Assembly, config =>
{
config.DocumentPath = documentpath;
});

app.UseSwaggerUi3(typeof(Startup).GetTypeInfo().Assembly, config =>
{
config.SwaggerRoutes.Add(new SwaggerUi3Route(versionWithV, documentpath));
});

However, this works, does not "hijack" site root:
app.UseSwaggerUi3(typeof(Startup).GetTypeInfo().Assembly, config =>
{
config.DocumentPath = documentpath;
});

Using version 12.0.4

done question

Most helpful comment

I have the exact same problem. I need to use SwaggerRoutes in UseSwaggerUi3() to add multiple documents, but if I do that the site root gets "hijacked" and returns swagger.json instead of hitting the default controller.

@svenerp Did you ever find a solution?
@RicoSuter Can you please have look?

Thanks!

I solved it, the key was to use the correct overload of UseSwaggerUi3() so it ONLY adds the Swagger UI to the OWIN pipeline.

Correct:

app.UseSwaggerUi3(config =>
{
config.Path = "/swagger";
config.SwaggerRoutes.Add(new SwaggerUi3Route(versionWithV, documentpath));
});

Not correct:

app.UseSwaggerUi3(typeof(Startup).Assembly, config =>
{
config.Path = "/swagger";
config.SwaggerRoutes.Add(new SwaggerUi3Route(versionWithV, documentpath));
});

All 6 comments

Shouldnt it be:

app.UseSwagger(typeof(Startup).GetTypeInfo().Assembly, config =>
{
     config.Path = documentpath;
});
app.UseSwaggerUi3(config =>
{
     config.Path = "/swagger";
     config.SwaggerRoutes.Add(new SwaggerUi3Route(versionWithV, documentpath));
});

I can not find a property "Path" here:
app.UseSwagger(typeof(Startup).GetTypeInfo().Assembly, config =>
{
config.Path = documentpath;
});

If I use DocumentPath there, and change the other part to
app.UseSwaggerUi3(config =>
{
config.Path = "/swagger";
config.SwaggerRoutes.Add(new SwaggerUi3Route(versionWithV, documentpath));
});

I still get swagger.json when I go to the root.

I have the exact same problem. I need to use SwaggerRoutes in UseSwaggerUi3() to add multiple documents, but if I do that the site root gets "hijacked" and returns swagger.json instead of hitting the default controller.

@svenerp Did you ever find a solution?
@RicoSuter Can you please have look?

Thanks!

I have the exact same problem. I need to use SwaggerRoutes in UseSwaggerUi3() to add multiple documents, but if I do that the site root gets "hijacked" and returns swagger.json instead of hitting the default controller.

@svenerp Did you ever find a solution?
@RicoSuter Can you please have look?

Thanks!

I solved it, the key was to use the correct overload of UseSwaggerUi3() so it ONLY adds the Swagger UI to the OWIN pipeline.

Correct:

app.UseSwaggerUi3(config =>
{
config.Path = "/swagger";
config.SwaggerRoutes.Add(new SwaggerUi3Route(versionWithV, documentpath));
});

Not correct:

app.UseSwaggerUi3(typeof(Startup).Assembly, config =>
{
config.Path = "/swagger";
config.SwaggerRoutes.Add(new SwaggerUi3Route(versionWithV, documentpath));
});

UseSwaggerUi3 with assembly is marked deprecated, right?

UseSwaggerUi3 with assembly is marked deprecated, right?

No, it's not. But it should be :)

Was this page helpful?
0 / 5 - 0 ratings