Aspnetcore.docs: Missing details of how to use NSwag with Redoc

Created on 20 Apr 2019  Â·  8Comments  Â·  Source: dotnet/AspNetCore.Docs

Page mentions that NSwag can be used as an alternative, and that it also generates/supports Redoc.

But there is no explanation how to do that (i.e. use redoc).


Document details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Source - Docs.ms doc-enhancement

Most helpful comment

Hi lonix1:

  1. You can find about "AspNetCore Middleware" and how add ReDoc:
    https://github.com/RicoSuter/NSwag/wiki/AspNetCore-Middleware
  1. Best example is here:
    https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Sample.NETCore20/Startup.cs

All 8 comments

Hi lonix1:

  1. You can find about "AspNetCore Middleware" and how add ReDoc:
    https://github.com/RicoSuter/NSwag/wiki/AspNetCore-Middleware
  1. Best example is here:
    https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Sample.NETCore20/Startup.cs

Or just add app.UseReDoc() and access via /redoc

Most simple setup for a .net core 3.0 web api project:

     public void ConfigureServices(IServiceCollection services)
        {          
            services.AddControllers();
            services.AddSwaggerDocument(config =>
            {
                config.PostProcess = document =>
                {
                    document.Info.Version = "v1";
                    document.Info.Title = "ToDo API";
                    document.Info.Description = "A simple ASP.NET Core web API";
                    document.Info.TermsOfService = "None";
                    document.Info.Contact = new NSwag.OpenApiContact
                    {
                        Name = "YourName",
                        Email = string.Empty,
                        Url = "https://twitter.com/YourName"
                    };
                    document.Info.License = new NSwag.OpenApiLicense
                    {
                        Name = "Use under LICX",
                        Url = "https://example.com/license"
                    };
                };
            });
        }

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            #region swagger

            // URLs: 
            // - http://localhost:65384/swagger/v1/swagger.json
            // - http://localhost:65384/swagger
            // - http://localhost:65384/redoc
            // - http://localhost:65384/openapi
            // - http://localhost:65384/openapi_redoc

            //app.UseOpenApi();
            app.UseSwaggerUi3();
            //app.UseReDoc();

            #endregion//// Add OpenAPI and Swagger middlewares to serve documents and web UIs
...
..
.

Only the following urls do not work:
// URLs:
// - http://localhost:xxxx/redoc --> This localhost page can’t be found
// - http://localhost:xxxx/openapi --> This localhost page can’t be found
// - http://localhost:xxxx/openapi_redoc --> This localhost page can’t be found

Access swagger: --> https://localhost:{YourPort}/swagger

To my knowledge, only app.UseSwaggerUI3() and app.UseReDoc() offer a web based user interface. For ReDoc, you'll need to configure the end-point.

Please see https://github.com/RicoSuter/NSwag/blob/ca33499f4d5c8b90423778d14b1dfa936d251ac7/src/NSwag.Sample.NETCore22/Startup.cs.

Maybe the default route of UseReDoc is also /swagger?

Installing Swashbuckle.AspNetCore.Redoc helps regarding Redoc. The Openapi urls still don't work.
Adapted my code to look like the above.

In order to create an endpoint for Redoc, you'll need to configure it in your Startup.cs. For example:

app.UseReDoc(config => { config.Path = "/redoc"; config.DocumentPath = "/swagger/v1/swagger.json"; });

Reference: https://github.com/RicoSuter/NSwag/blob/ca33499f4d5c8b90423778d14b1dfa936d251ac7/src/NSwag.Sample.NETCore22/Startup.cs#L54

EDIT

Here's another example:
https://github.com/RicoSuter/NSwag/blob/1a6c959bdb41b41f2f534d3e61a311f1cf0de6f7/src/NSwag.Sample.NETCore20/Startup.cs#L104

Moved to Master issue, check here first. #16879

Was this page helpful?
0 / 5 - 0 ratings

Related issues

YeyoCoder picture YeyoCoder  Â·  3Comments

Rick-Anderson picture Rick-Anderson  Â·  3Comments

sonichanxiao picture sonichanxiao  Â·  3Comments

royshouvik picture royshouvik  Â·  3Comments

wgutierrezr picture wgutierrezr  Â·  3Comments