Using Swagger UI and ReDoc in ASP.NET Core 3.0

on Monday, December 9, 2019

I was working with the Swashbuckle.AspNetCore library and wanted to play around with both the Swagger-UI active documentation endpoint and the ReDoc active documentation endpoint at the same time. I wanted to compare the two to see which was easier to use. (It turns out they’re both really easy to use.)

But, I ran into a problem, both UI endpoints wanted to use OpenAPI json document to pull in the Web APIs definition. And, the default configuration that comes with Swashbuckle.AspnetCore will generate that document at “swagger/v1/swagger.json”. (You can change this path using the .RouteTemplate property during configuration.)

https://your.domain.com/subapp/swagger/v1/swagger.json

This wasn’t a problem with the Swagger-UI system because the .UseSwaggerUI configuration has a default base path of “swagger”. This means all you need to do to configure is call .SwaggerEndpoint(“v1/swagger.json”, “My API V1”) and it will find the correct path.

Swagger-UI’s default path is “swagger”, so adding “v1/swagger.json” will create “swagger/v1/swagger.json”.

However, the .UseReDoc configuration has a base path of “api-docs”. Which means you shouldn’t be able to find the default json documentation path under it.

You shouldn’t be able to … but in a very interesting twist, the configuration does allow for relative pathing. Which means you can use “..” to escape the default pathing. This was great! So, you can use a relative path, “../swagger/v1/swagger.json” to reference it.

ReDocs default path is “api-docs”, so you can escape it using relative path like “../swagger/v1/swagger.json”. This will create the expected “swagger/v1/swagger.json”

Here’s an example:

5 comments:

Anonymous said...

Steve, thank you very much for sharing this. Was very helpfull as I was facing exact the same situation! You saved my day!

jefferson said...

Thank you Steve!

Anonymous said...

Hi Good evening! Can you please tell me how to render model name replacing in my redoc api documentation

smaglio81 said...

Yeah, I'm not sure. I mostly used Swashbuckle integration for the projects I did. Swashbuckle integrates with Microsoft's ApiExplorer (https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.ApiExplorer/) library. So, with Swashbuckle I could manipulate the ApiExplorer information to influence what was generated.

Anonymous said...

Thank you so much Steve

Post a Comment


Creative Commons License
This site uses Alex Gorbatchev's SyntaxHighlighter, and hosted by herdingcode.com's Jon Galloway.