Cristina Pereira Cunha
Cristina Pereira Cunha

Reputation: 663

How to set Swagger as default start page?

How do I set Swagger as the default start page in ABP template instead of /Account/Login?

I'm using ASP.NET MVC 5.x + Angular 1.x.

Update

Current code:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    //ASP.NET Web API Route Config
    routes.MapHttpRoute(
        name: "swagger_root",
        routeTemplate: "",
        defaults: null,
        constraints: null,
        handler: new RedirectHandler((message => message.RequestUri.ToString()), "swagger"));

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

Everything is still working fine, except Module Zero's "api/Account/Authenticate" request that has broken, showing:

The resource cannot be found.

Upvotes: 55

Views: 65754

Answers (8)

Sindhoor
Sindhoor

Reputation: 548

For .Net core 3.1, change the configuration in launchSettings.json file

Search for launchSettings.json file Change the property "launchUrl" value to "swagger". refer below:

 "profiles": {
"IIS Express": {
  "commandName": "IISExpress",
  "launchBrowser": true,
  "launchUrl": "swagger",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
},
"AfterPayAPI": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "swagger",
  "applicationUrl": "https://localhost:5001;http://localhost:5000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

Upvotes: 7

Ian Vink
Ian Vink

Reputation: 68790

In Asp .Net Web Api Core 3.x just do this (RoutePrefix):

        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Baha'i Prayers API");
            c.InjectStylesheet("/swagger/custom.css");
            c.RoutePrefix = String.Empty;
        });

Upvotes: 11

JCisar
JCisar

Reputation: 2674

I know the main question was for ASP.Net MVC, but for ASP.Net Core a good solution from this answer on a similar question (using SwaggerUI): https://stackoverflow.com/a/50127631/1179562

app.UseSwaggerUI(c =>
{
    c.RoutePrefix = "";
    ...
};

Upvotes: 25

Silvair L. Soares
Silvair L. Soares

Reputation: 1066

What worked well for me, both in Visual Studio, IIS Express and also in IIS. It was to create a controller with the following content:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication.Controllers
{
    /// <summary>
    /// Controller to display API documentation in Swagger format
    /// </summary>
    [Route("")]
    [ApiExplorerSettings(IgnoreApi = true)]
    public class DocsController : Controller
    {
        [Route("docs"), HttpGet]
        [AllowAnonymous]
        public IActionResult ReDoc()
        {
            return View();
        }

        [Route(""), HttpGet]
        [AllowAnonymous]
        public IActionResult Swagger()
        {
            return Redirect("~/swagger");
        }
    }
}

Note: Editing the launchsettings.json file worked well in Visual Studio, but insists that it doesn't work as expected when hosting the application on IIS.

In this way, I found it cleaner than creating a lot of configuration in several different locations.

Upvotes: 1

miguelangelvelarde
miguelangelvelarde

Reputation: 109

I went to the Solution Explorer Panel > Properties. In there I found a file called launchsettings.json.

In this file I changed the value for the "launchUrl" parameter to "swagger/index.html" in all the sections where I found it.

It´s works for me.

Upvotes: 8

JimbobTheSailor
JimbobTheSailor

Reputation: 1623

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

Default URL In Resful API dot net core 2.2

Upvotes: 51

fix
fix

Reputation: 2205

I was able to do this by filling in the Start Action of the project's properties with Specific Page > /swagger/ui/index

enter image description here

NOTE: I am using Swashbuckle from NuGet

Upvotes: 7

aaron
aaron

Reputation: 43098

Add this routing in RouteConfig.cs as commented out here:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    //ASP.NET Web API Route Config
    routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
        );

    // Set Swagger as default start page
    /*
    routes.MapHttpRoute(
        name: "swagger_root",
        routeTemplate: "",
        defaults: null,
        constraints: null,
        handler: new RedirectHandler((message => message.RequestUri.ToString()), "swagger"));
    */

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

Upvotes: 22

Related Questions