51

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.

aaron
  • 39,695
  • 6
  • 46
  • 102
  • For quick solution, please refer to this: [view my suggested solution](https://stackoverflow.com/questions/30028736/how-to-use-swagger-as-welcome-page-of-iappbuilder-in-webapi/52699198#52699198) – Abdul Mueed Shahid Oct 08 '18 at 09:32
  • On that same SO post right above @AbdulMueedShahid post is a much better solution for ASP.Net Core apps. https://stackoverflow.com/a/50127631/1179562 – JCisar Jul 28 '20 at 15:55

8 Answers8

50

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

JimbobTheSailor
  • 1,441
  • 1
  • 12
  • 21
  • 6
    I just updated to AspNet Core 3.1, and the forward slash in the above solution is not necessary. I'm not sure if this is specific to my configuration, or in general? – JimbobTheSailor Mar 25 '20 at 22:33
  • This is how you use program x to do this. What if you are using program A e.g. Rider? What if you're working directly from the code? – Urasquirrel Apr 27 '21 at 19:52
  • @Urasquirrel, this answer is just a UI for the `Properties\launchsettings.json` file. You can manually edit that file. I don't know the best steps for Rider. – Lee Grissom Mar 16 '22 at 03:59
24

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 = "";
    ...
};
JCisar
  • 2,584
  • 2
  • 29
  • 28
  • This is simple and better. Just because the other provided solutions require us to make configuration settings for routes. Remember you also need to remove launchUrl from launchSettings.json if there is any provided otherwise, this will not work. – Malik Khalil May 17 '21 at 08:38
22

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 }
    );
}
aaron
  • 39,695
  • 6
  • 46
  • 102
9

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;
        });
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
7

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.

7

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

fix
  • 1,425
  • 16
  • 27
  • 1
    It works but saves the url in user specific file. So, every developer has to do it manually. – JGV Sep 21 '20 at 20:44
4

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"
  }
}
Sindhoor
  • 514
  • 3
  • 14
1

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.

Silvair L. Soares
  • 1,018
  • 12
  • 28