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

后端 未结 5 799
孤街浪徒
孤街浪徒 2020-12-14 07:18

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

http://localhost:/swagger/ui

I would

相关标签:
5条回答
  • 2020-12-14 07:36
    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.
    0 讨论(0)
  • 2020-12-14 07:40

    You can do this as well in Config

    app.UseSwaggerUI(c =>
                {
                    c.RoutePrefix = string.Empty;
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "MY API");
                });
    
    0 讨论(0)
  • 2020-12-14 07:47

    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/

    0 讨论(0)
  • 2020-12-14 07:53

    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

    0 讨论(0)
  • 2020-12-14 07:56

    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"
          }
        }
      }
    
    0 讨论(0)
提交回复
热议问题