16

I have a WebApi project with Swashbuckle installed onto it.

In default setup, I must open in browser http://localhost:56131/swagger/ui/index to view my operations description and test page. I want it to be accessible from root of the site: http://localhost:56131/. How can I achieve this?

v.karbovnichy
  • 2,735
  • 2
  • 29
  • 43

8 Answers8

31

Influenced by this answer to similar question, slightly modified code:

public class WebApiConfig
{
    public static void Configure(IAppBuilder app)
    {
        var httpConfig = new HttpConfiguration();

        // Attribute routing
        config.MapHttpAttributeRoutes();

        // Redirect root to Swagger UI
        config.Routes.MapHttpRoute(
            name: "Swagger UI",
            routeTemplate: "",
            defaults: null,
            constraints: null,
            handler: new RedirectHandler(SwaggerDocsConfig.DefaultRootUrlResolver, "swagger/ui/index"));

        // Configure OWIN with this WebApi HttpConfiguration
        app.UseWebApi(httpConfig);
    }
}

This way it is not necessary to create new WebAPI controller as so did @bsoulier in his answer.

This solution is based on already existing class RedirectHandler in Swashbuckle.Core assembly.

Sergey Shuvalov
  • 1,980
  • 2
  • 15
  • 19
v.karbovnichy
  • 2,735
  • 2
  • 29
  • 43
10

If you for sure use a Web API project, you can:

[Route(""), HttpGet]
[ApiExplorerSettings(IgnoreApi = true)]
public HttpResponseMessage RedirectToSwaggerUi()
{
    var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Found);
    httpResponseMessage.Headers.Location = new Uri("/swagger/ui/index", UriKind.Relative);
    return httpResponseMessage;
}

Note the use of ApiExplorerSettings attribute, so that it doesn't show up in the Swagger definition.

Benjamin Soulier
  • 1,975
  • 1
  • 16
  • 27
4

Even simpler variant of the above answer:

public class DefaultController : Controller
{
    [Route(""), HttpGet]
    [ApiExplorerSettings(IgnoreApi = true)]
    public RedirectResult RedirectToSwaggerUi()
    {
        return Redirect("/swagger/");
    }
}

The simpler, the better! This one works for me.

2

For a RESTFUL API in ASP Net Core > 2.2, ,set the default URL in Project/Properties/ Debug

enter image description here

And launchSettings.json will be automaticlly updated:

"profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger/index.html",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
Dan
  • 360
  • 4
  • 18
1

For .net core in the properties launchsettings.json modify profiles and api sections to point to swagger.

profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "authapi": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/swagger",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
Klaus Gütter
  • 6,283
  • 4
  • 24
  • 30
Hal Davis
  • 11
  • 1
0

if the task allows you, in _layout.chtml just add follow line to head:

<meta http-equiv="refresh" content="0; URL='/swagger'" />

this will redirect you on swagger/index after page was loaded

0

Change the value of launchUrl = "swagger/index.html" in launchSetting.Json in asp.net core 2.1/2.2

"ProjectName": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger/index.html",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:44333"
    }
San Jaisy
  • 9,586
  • 15
  • 93
  • 162
-1

If you are running dotnet core, just goto properties->launchsettings.json and comment out or remove the launchUrl and set the swagger RoutePrefix to string.Empty.