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

26,016

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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"
      }
Share:
26,016
v.karbovnichy
Author by

v.karbovnichy

I am MS technology stack developer with primary focus on C#/SQL

Updated on May 06, 2020

Comments

  • v.karbovnichy
    v.karbovnichy almost 4 years

    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?