How to set Swagger as default start page?

All we need is an easy explanation of the problem, so here it is.

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.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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

Method 2

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 }
    );
}

Method 3

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 = "";
    ...
};

Method 4

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.

Method 5

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;
        });

Method 6

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

Method 7

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.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply