This post contains a few tips and tricks that can help you transform your swagger UI into an interactive documentation. If you don’t yet know how to install swagger in ASP.NET Core, this post will help you Get started with Swagger and ASP.NET Core.

How to change the URL of the Swagger UI

By default, the Swagger UI can be found at http://localhost:/swagger. To change the path and set, for example, the Swagger UI at the app’s root, use:

app.UseSwaggerUI(c =>{ c.SwaggerEndpoint(“/swagger/v1/swagger.json”, “My API V1”); c.RoutePrefix = string.Empty;});

view raw

swagger_RoutePrefix.cs hosted with ❤ by GitHub

How to revert Swagger JSON to version 2.0

By default, Swashbuckle generates and exposes Swagger JSON in version 3.0 of the specification -officially called the OpenAPI Specification. To support backwards compatibility, you can opt into exposing JSON in the 2.0 format instead:

app.UseSwagger(c => { c.SerializeAsV2 = true; });

view raw

swagger_SerializeAsV2.cs hosted with ❤ by GitHub

How to add author, license, and description

If you want, you can pass information such as the author, license, and description, using the OpenApiInfo object:

services.AddSwaggerGen(c =>{ c.SwaggerDoc(“v1”, new OpenApiInfo { Version = “v1”, Title = “The .NET Lab API”, Description = “An imaginary API of my blog”, TermsOfService = new Uri(https://blog.georgekosmidis.net/privacy-policy/), Contact = new OpenApiContact { Name = “George Kosmidis”, Email = string.Empty, Url = new Uri(https://georgekosmidis.net), }, License = new OpenApiLicense { Name = “Use under MIT”, Url = new Uri(https://blog.georgekosmidis.net/privacy-policy/), } });});

#asp.net core #aspnetcore #dotnet #swagger #swashbuckle

Swagger in ASP.NET Core: Tips & Tricks

blog.georgekosmidis.net

Swagger in ASP.NET Core: Tips & Tricks

Transform your swagger UI into an interactive documentation. This post contains a few tips and tricks that can help you transform your swagger UI into an interactive documentation. If you don’t yet know how to install swagger in ASP.NET Core, this post will help you <span style="color: rgb(3, 102, 214);">Get started with Swagger and ASP.NET Core</span>. ## How to change the URL of the Swagger UI By default, the Swagger UI can be found at `http://localhost:/swagger`. To change the path and set, for example, the Swagger UI at the app’s root, use: app.<span style="color: rgb(111, 66, 193);">UseSwaggerUI</span>(c <span style="color: rgb(215, 58, 73);">=></span>{ c.<span style="color: rgb(111, 66, 193);">SwaggerEndpoint</span>(<span style="color: rgb(3, 47, 98);">"/swagger/v1/swagger.json"</span>, <span style="color: rgb(3, 47, 98);">"My API V1"</span>); c.RoutePrefix <span style="color: rgb(215, 58, 73);">=</span> string.Empty;}); [view raw](https://gist.github.com/georgekosmidis/f5b4a75fc556a84fa4f7d224094b0a1d/raw/68a44d66e9d9fd79422597c3b39fbca89d0d10fa/swagger_RoutePrefix.cs) [swagger_RoutePrefix.cs](https://gist.github.com/georgekosmidis/f5b4a75fc556a84fa4f7d224094b0a1d#file-swagger_routeprefix-cs) hosted with ❤ by [GitHub](https://github.com/) ## How to revert Swagger JSON to version 2.0 By default, Swashbuckle generates and exposes Swagger JSON in version 3.0 of the specification -officially called the <span style="color: rgb(3, 102, 214);">OpenAPI Specification</span>. To support backwards compatibility, you can opt into exposing JSON in the 2.0 format instead: app.<span style="color: rgb(111, 66, 193);">UseSwagger</span>(c <span style="color: rgb(215, 58, 73);">=></span> { c.SerializeAsV2 <span style="color: rgb(215, 58, 73);">=</span> <span style="color: rgb(0, 92, 197);">true</span>; }); [view raw](https://gist.github.com/georgekosmidis/f5b4a75fc556a84fa4f7d224094b0a1d/raw/68a44d66e9d9fd79422597c3b39fbca89d0d10fa/swagger_SerializeAsV2.cs) [swagger_SerializeAsV2.cs](https://gist.github.com/georgekosmidis/f5b4a75fc556a84fa4f7d224094b0a1d#file-swagger_serializeasv2-cs) hosted with ❤ by [GitHub](https://github.com/) ## How to add author, license, and description If you want, you can pass information such as the author, license, and description, using the `OpenApiInfo` object: services.<span style="color: rgb(111, 66, 193);">AddSwaggerGen</span>(c <span style="color: rgb(215, 58, 73);">=></span>{ c.<span style="color: rgb(111, 66, 193);">SwaggerDoc</span>(<span style="color: rgb(3, 47, 98);">"v1"</span>, <span style="color: rgb(215, 58, 73);">new</span> <span style="color: rgb(111, 66, 193);">OpenApiInfo</span> { Version <span style="color: rgb(215, 58, 73);">=</span> <span style="color: rgb(3, 47, 98);">"v1"</span>, Title <span style="color: rgb(215, 58, 73);">=</span> <span style="color: rgb(3, 47, 98);">"The .NET Lab API"</span>, Description <span style="color: rgb(215, 58, 73);">=</span> <span style="color: rgb(3, 47, 98);">"An imaginary API of my blog"</span>, TermsOfService <span style="color: rgb(215, 58, 73);">=</span> <span style="color: rgb(215, 58, 73);">new</span> <span style="color: rgb(111, 66, 193);">Uri</span>(<span style="color: rgb(3, 47, 98);">"https://blog.georgekosmidis.net/privacy-policy/"</span>), Contact <span style="color: rgb(215, 58, 73);">=</span> <span style="color: rgb(215, 58, 73);">new</span> <span style="color: rgb(111, 66, 193);">OpenApiContact</span> { Name <span style="color: rgb(215, 58, 73);">=</span> <span style="color: rgb(3, 47, 98);">"George Kosmidis"</span>, Email <span style="color: rgb(215, 58, 73);">=</span> string.Empty, Url <span style="color: rgb(215, 58, 73);">=</span> <span style="color: rgb(215, 58, 73);">new</span> <span style="color: rgb(111, 66, 193);">Uri</span>(<span style="color: rgb(3, 47, 98);">"https://georgekosmidis.net"</span>), }, License <span style="color: rgb(215, 58, 73);">=</span> <span style="color: rgb(215, 58, 73);">new</span> <span style="color: rgb(111, 66, 193);">OpenApiLicense</span> { Name <span style="color: rgb(215, 58, 73);">=</span> <span style="color: rgb(3, 47, 98);">"Use under MIT"</span>, Url <span style="color: rgb(215, 58, 73);">=</span> <span style="color: rgb(215, 58, 73);">new</span> <span style="color: rgb(111, 66, 193);">Uri</span>(<span style="color: rgb(3, 47, 98);">"https://blog.georgekosmidis.net/privacy-policy/"</span>), } });});

3.10 GEEK