22

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
  • 3,183
  • 2
  • 36
  • 47

9 Answers9

35

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
  • 2,098
  • 2
  • 17
  • 21
v.karbovnichy
  • 3,183
  • 2
  • 36
  • 47
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
  • 2,223
  • 1
  • 18
  • 30
10

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.

Gunnar Siréus
  • 162
  • 1
  • 6
3

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
  • 11,151
  • 6
  • 31
  • 36
Hal Davis
  • 31
  • 1
3

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
  • 448
  • 10
  • 20
  • This doesn't answer the question. The question, is how to automatically redirect when navigating to the root. Not how to launch to swagger. This doesn't help if a service is already running and he wants to simply navigate to the root to see the api. – claudekennilol Apr 25 '22 at 22:06
2

If you're using .net core, go to launchSetting.json and in the line launchUrl that is by default "api/values" change it for "swagger/ui/index" or "swagger/index"

"IIS Express": {
  "commandName": "IISExpress",
  "launchBrowser": true,
  "launchUrl": "swagger/index.html",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
},
1

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
  • 15,327
  • 34
  • 171
  • 290
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

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.

Lawrence
  • 61
  • 4