0

I'm struggling with getting the configuration for Swagger/Swashbuckle correct in an Asp.Net core 2.0 web api. I've followed the examples, which all work brilliantly when working at the root folder/localhost. As many others have pointed out, I too am seeing different behavior when working in a virtual folder on the server. I've examined this question - IIS site within virtual directory Swagger UI end point which is similar, but the solution provided there is not working.

My startup.cs file has the following block for configuring services:

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors();

        services.AddSwaggerGen(c =>
        {
            c.DescribeAllEnumsAsStrings();
            c.IncludeXmlComments(string.Format(@"{0}\EmployeeService.xml", System.AppDomain.CurrentDomain.BaseDirectory));
            c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "Employee Service"
            });
        });
...
}

And my Configure method looks like this:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

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

        app.UseMvc();
    }

I've tried this with and without adding the RoutePrefix to the SwaggerUI section.

As I mentioned, I'm running .Net Core 2.0.3, I have the Nuget package Swashbuckle.AspNetCore 2.3.0 referenced.

What I get in the app regardless of what I do with the path is a 404 on the /swagger/v1/swagger.json file when I try to access {server}/{virtualdirectory}/swagger. The UI loads, but it won't load the json file, as it always tries to find it at server root.

Can someone point me in the right direction?

reallyJim
  • 1,336
  • 2
  • 16
  • 32

2 Answers2

0

You must check your [http] route , dont use [routes] before http`s tag.

you must add a api route on the top and remove all routes before them Http:

[Route("api/[controller]")]
public class asteriskAPI:Controller
{ ........

}

and like this:

[HttpGet]
    public ActionResult<List<ast_cel>> GetAll()
    {
  ...
   }
MsDeveloper
  • 17
  • 1
  • 9
0

You need to change your app.UseSwaggerUI method to this

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

Note the period.

Source: https://learn.microsoft.com/en-us/samples/aspnet/aspnetcore.docs/getstarted-swashbuckle-aspnetcore/

Nitromite
  • 1
  • 2