Swashbuckle.aspnetcore: swagger route conflict with default http route in Asp.Net Core 2.0

Created on 22 Sep 2017  路  2Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

I use "dotnet core new reactredux" to create my Asp.Net SPA application, everything go well before I introduce Swashbuckle to my project.

A after I installed Swashbuckle into my project (followed the Get-Start document), and I tried to access: http:127.0.0.1:5000/swagger/ , the browser don't show the API document. then I try to found out what is wrong here, because I use to successfully run Swashbuckle in my other Asp.Net Core project. and this is what I found:

the reason of swagger don't show up, is this code in Configure method of Startup.cs

          app.UseMvc(routes =>
            {
                routes.MapSpaFallbackRoute(
                    name: "spa-fallback",
                    defaults: new { controller = "Home", action = "Index" });
            });

if I comment out this code, and just use "app.UseMvc", it works.

    app.UseMvc();

the project is here: https://github.com/czihong/DotNetCoreFans/blob/master/Startup.cs

Most helpful comment

It looks like you solved the issue from your current Startup.cs file. Though for other users, you can fix it by adding the UseSwaggerUI above the UseMvc routing. I believe the MapSpaFallbackRoute is a catch all routing so it loads up the single page app when /swagger is used.

In version 1 of Swashbuckle I got around this issue by entering /swagger/index.htm but in version 2 you need to use the /swagger URL without the index.html.

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1");
});

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");

    routes.MapSpaFallbackRoute(
        name: "spa-fallback",
        defaults: new { controller = "Home", action = "Index" });
});

All 2 comments

It looks like you solved the issue from your current Startup.cs file. Though for other users, you can fix it by adding the UseSwaggerUI above the UseMvc routing. I believe the MapSpaFallbackRoute is a catch all routing so it loads up the single page app when /swagger is used.

In version 1 of Swashbuckle I got around this issue by entering /swagger/index.htm but in version 2 you need to use the /swagger URL without the index.html.

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1");
});

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");

    routes.MapSpaFallbackRoute(
        name: "spa-fallback",
        defaults: new { controller = "Home", action = "Index" });
});

This got me too - moving the UseMvc route configuration after the UseSwaggerUI fixed it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabeluci picture gabeluci  路  3Comments

vanillajonathan picture vanillajonathan  路  3Comments

TimmyGilissen picture TimmyGilissen  路  3Comments

m-demydiuk picture m-demydiuk  路  3Comments

flipchart picture flipchart  路  4Comments