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

8

83

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 is no similar method in ASP.NET Core:

.EnableSwagger(c =>
{
    c.RootUrl(req => myCustomBasePath);
});
Deflective answered 24/8, 2016 at 6:37 Comment(0)
B
83

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

Bauske answered 26/4, 2017 at 22:34 Comment(4)
the RoutePrefix alone isn't enough if you want to use URL prefix like /prefix/swagger/... for both Swagger UI and swagger.json file. You also need to set RouteTemplate property.Archaeozoic
@IvanSveshnikov Update: I think it works fine now on 2022 without the RouteTemplate . :) Thank you! :)Homozygote
@OmarMagdy it doesn't work for me on NET 7 VS2022, I had to add RouteTemplate.Publus
As of .net core 6 this only works when running where the .net core app is running at the root of the site... if you have a virtual directory in front of it (such as behind a proxy) it is still not working. I had to use the RouteTemplate to make it work behind a proxyHypercorrection
B
103

For ASP.NET Core 2 (and using Swashbuckle.AspNetCore.Swagger -Version 4.0.1), a couple things can be done for a full configuration of changing the default swagger UI base URL.

If you want to add "mycoolapi" to the beginning of your default swagger UI URL, like this: http://<server>/mycoolapi/swagger, then do the following:

In your Startup.cs Configure method:

    app.UseSwagger(c =>
    {
        c.RouteTemplate = "mycoolapi/swagger/{documentname}/swagger.json";
    });


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

Then, if you currently have your launchSettings to launch browser at swagger UI upon startup (for development purposes), update your launchSettings.json file profiles section similarly:

  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "mycoolapi/swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MyProject.Web": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "mycoolapi/swagger",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
Barouche answered 17/4, 2019 at 17:2 Comment(3)
what is your iisExpress applicationUrl value? .. I tried everything but for some reason I cannot make it to work .. In the end I want to host it in IIS ('localhost/appName/')Redstart
This should be accepted anwser. Set RouteTemplate is a must now in .net core. It was different in .net framework.Cuthbert
SwaggerEndPoint part should be like below for relative usage. c.SwaggerEndpoint("v1/swagger.json", "My Cool API V1");Margarito
B
83

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

Bauske answered 26/4, 2017 at 22:34 Comment(4)
the RoutePrefix alone isn't enough if you want to use URL prefix like /prefix/swagger/... for both Swagger UI and swagger.json file. You also need to set RouteTemplate property.Archaeozoic
@IvanSveshnikov Update: I think it works fine now on 2022 without the RouteTemplate . :) Thank you! :)Homozygote
@OmarMagdy it doesn't work for me on NET 7 VS2022, I had to add RouteTemplate.Publus
As of .net core 6 this only works when running where the .net core app is running at the root of the site... if you have a virtual directory in front of it (such as behind a proxy) it is still not working. I had to use the RouteTemplate to make it work behind a proxyHypercorrection
M
8

Other answers didn't work for me in .NET 5. Here is how I added 'docs' to the route

app.UseSwagger(c => c.RouteTemplate = "/swagger/docs/{documentName}/swagger.json");
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/docs/v2/swagger.json", "My API"));
Misplace answered 11/11, 2021 at 4:32 Comment(0)
B
3

You can do this as well in Config

app.UseSwaggerUI(c =>
            {
                c.RoutePrefix = string.Empty;
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "MY API");
            });
Bargello answered 14/11, 2018 at 12:40 Comment(1)
Congrats for posting exactly the same answer as the accepted one, but with the lines switched round ;-)Unify
B
2
  1. Open the launchSettings.json file.
  2. Under the "profiles" node depending on your setup you should have one or more profiles. In may case I had "IIS Express" and another with named with my project name (e.g WebApplication1 ), now changing the launchUrl entry to "launchUrl": "swagger" solved my problem.
  3. If this does not work and you have other profiles do the same and test.
Breezeway answered 12/7, 2018 at 11:56 Comment(0)
C
2

I had to dig through the original docs because all the above solutions did not work. This code in Startup.cs is what is working for me:

app.UseSwagger();
app.UseSwaggerUI(c => {
    c.SwaggerEndpoint($"./v1/swagger.json", "SapWeb v1");
});

My site is a subsite in IIS. So, at first, I had to set PathBase:

app.UsePathBase($"{pathbase}");
Creed answered 18/5, 2021 at 13:31 Comment(1)
What is your "pathbase" variable?Change
E
0

Using Dotnet/Aspnet8

app.UseSwagger(c =>
{
  c.RouteTemplate = "/MyCustomStuff/swagger/{documentName}/swagger.{json|yaml}";
});

should change the URL properly.


I found this by following UseSwagger..RouteTemplate to the source:

/// <summary>
/// Sets a custom route for the Swagger JSON/YAML endpoint(s). Must include the {documentName} parameter
/// </summary>
public string RouteTemplate { get; set; } = "swagger/{documentName}/swagger.{json|yaml}";
Ell answered 13/12, 2023 at 12:53 Comment(0)
D
-3

The UseSwaggerUi() extension method to enable the middleware in the Configure method of the StartUp class takes two variables. A baseRoute which is on swagger/ui by default, and swaggerUrl which is on swagger/v1/swagger.json by default. Simply provide a different baseRoute.

//Swagger will be available under '/api' url
app.UseSwaggerUi("api");

If people would like to learn more about configuring Swagger for ASP.NET Core, I've written a blogpost to get started: https://dannyvanderkraan.wordpress.com/2016/09/09/asp-net-core-1-0-web-api-automatic-documentation-with-swagger-and-swashbuckle/

Detradetract answered 24/8, 2016 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.