2

I have a .NetCore Web API project. And I am trying to use Swagger on it. All the configuration looks good but when I run my project I got an 404 error, page not found.

Here is my code:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton(_config);
        services.AddTransient<IRestHelper, RestHelper>();
        // Add framework services.
        services.AddApplicationInsightsTelemetry(_config);

        services.AddMvc();
        services.AddSwaggerGen(config =>
        {
            config.SwaggerDoc("v1", new Info { Title = "Slack Relay API", Version = "v1" });
        });
    }


     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(_config.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseApplicationInsightsRequestTelemetry();

        app.UseApplicationInsightsExceptionTelemetry();
        app.UseMvc();
        app.UseSwagger(c =>
        {
            c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value);
        });

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

My web API base url is: http://localhost:88/ So when I try to open this URL I get a 404: http://localhost:88/swagger/ui

I am using NetCore 1.0.1 and In the project.json I have this line to import Swagger: "Swashbuckle.AspNetCore": "1.0.0"

Any help please? thanks

MarcosF8
  • 1,848
  • 5
  • 19
  • 33

2 Answers2

5

You are entering the wrong URL in the browser. With Swagger 1.0.0 RTM the default url changed from http://localhost:88/swagger/ui to http://localhost:88/swagger

Tseng
  • 61,549
  • 15
  • 193
  • 205
3

You need to remember that order of middlewares is important: MVC midleware should be the last one, as it tries to process all requests. As you do not have controller's action mapped to /swagger/ui you receive 404 error. Modify Configure method to:

    app.UseSwagger(c =>
    {
        c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value);
    });

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

    app.UseMvc();

Moreover, it is better to replace app.UseMvc() with something like the following, so MVC middleware will not try to process any requests related to swagger (requests to URL started with /swagger):

app.MapWhen(x => !x.Request.Path.Value.StartsWith("/swagger"), builder =>
{
    builder.UseMvc();
});
Set
  • 47,577
  • 22
  • 132
  • 150
  • Thanks for that. It is extrange, I did exactly the changes you mentioned and it is still not work. This url shows the json of Swagger http://localhost:88/swagger/v1/swagger.json . So getting the json means the configuration was done well? But I still have the 404 for this url http://localhost:88/swagger/ui .Any idea? – MarcosF8 Jul 02 '17 at 14:55
  • I got my stuff working thanks to you and the other response. I would like to put two right responses, but looks like it is not possible. Thanks so much ! – MarcosF8 Jul 02 '17 at 15:10