Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change base url of Swagger in ASP.NET core

By default when you enable swagger in ASP.NET Core project it's available on url:

http://localhost:<random_port>/swagger/ui 

I would like to use a different base url instead of /swagger/ui. How/where can i configure that?

I found that for older versions you can configure the RootUrl but there aren't similiar method in ASP.NET Core:

.EnableSwagger(c => {     c.RootUrl(req => myCustomBasePath); }); 
like image 595
Mariusz Jamro Avatar asked Aug 24 '16 06:08

Mariusz Jamro


People also ask

How do you set the base path in Swagger?

In OpenAPI 3.0, you can use an array of server elements to specify one or more base URLs for your API. You can register the servers using the annotation @Server: in @OpenAPIDefinition (for all operations) in @Operation (for a single operation)

What is the Swagger default URL?

Add and configure Swagger middleware Launch the app and navigate to https://localhost:<port>/swagger/v1/swagger.json . The generated document describing the endpoints appears as shown in OpenAPI specification (openapi. json). The Swagger UI can be found at https://localhost:<port>/swagger .

What is the default base URL for Swagger?

By default when you enable swagger in ASP.NET Core project it's available on url: http://localhost:<random_port>/swagger/ui. I would like to use a different base url instead of /swagger/ui.

How to create Swagger in ASP NET Core?

In this article, you will learn about swagger in .NET Core. Open Visual Studio and select “Create new project. Select ASP.Net Core Web Application from the templates displayed.

How do I add a route prefix to a Swagger UI?

To add a route prefix to swagger and swagger ui is a pretty quick code change. To get a ASP.Net Core Web Api up and running, create a new project and select ASP.Net Core Web Application (.Net Core). Once the project is created you will nuget in Swashbuckle.AspNetCore:

How to run Swagger API locally and launch Swagger UI?

Press F5 to run the API locally and to launch the Swagger UI just hit the http://localhost:<port_number>/swagger/index.html URL in the browser. The Swagger UI for above controller looks as follows, Hit the http://localhost:<port_number>/swagger/v1/swagger.json URL in the browser. We can see the OpenAPI specification (openapi.json).


1 Answers

The new swagger version provides you with a property called RoutePrefix.

app.UseSwaggerUI(c => {     c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");     c.RoutePrefix = "docs"; }); 

Should work for .net core

like image 68
maxspan Avatar answered Sep 23 '22 23:09

maxspan