v.karbovnichy
v.karbovnichy

Reputation: 3314

How to redirect from root url to /swagger/ui/index?

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?

Upvotes: 23

Views: 42430

Answers (9)

Carlos de Jesus Baez
Carlos de Jesus Baez

Reputation: 395

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"
  }
},

Upvotes: 2

Dan
Dan

Reputation: 468

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"
      }
    },

Upvotes: 4

Lawrence
Lawrence

Reputation: 61

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.

Upvotes: 0

San Jaisy
San Jaisy

Reputation: 17118

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"
    }

Upvotes: 1

Hal Davis
Hal Davis

Reputation: 31

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"
      }

Upvotes: 3

VanavaraVL
VanavaraVL

Reputation: 1

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

Upvotes: 0

Gunnar Sir&#233;us
Gunnar Sir&#233;us

Reputation: 192

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.

Upvotes: 11

v.karbovnichy
v.karbovnichy

Reputation: 3314

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.

Upvotes: 35

Benjamin Soulier
Benjamin Soulier

Reputation: 2263

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.

Upvotes: 10

Related Questions