Hi
I have read many posts on this issue but none of them seem to relate to the ASP.NET Core implementation.
Swashbuckle works perfectly on a development machine but when deployed to a server in a virtual directory (IIS application) it doesn't add the virtual folder to the swagger.json url and I get:
Can't read swagger JSON from http://localhost/swagger/v1/swagger.json
If I insert the virtual directory in the url in the box at the top of the page everything works fine (including the "Try It Out" buttons), but this is not user friendly for those who will be working with the api.
Thanks very much to anyone who can shed any light on this.
Ian Elsinga
Hi again, sorry to bump this but I have spent hours on this and cannot resolve it. I attempted to provide the routeTemplate in the app.UseSwagger call but there was no change. In addition to the above issue, if I use servername instead of localhost it doesn't display anything but a green bar and a blank box.
Does swashbuckle for asp.net core not work at all in a non-dev environment?
Thanks very very much for any assistance or pointers in the right direction.
It's not the fact that it's a non-Dev environment, it's because you're using a virtual directory outside of your dev environment.
At the bottom of the readme, you'll see links to examples and there should be one there called VirtualDirectory. Check out its start up. You can Ignore the app.Map stuff as that's just for simulating a vdir but checkout the explicit setting of swaggerUrl in the call to UseSwaggerUi. Try this and it should work:
https://github.com/domaindrivendev/Ahoy/blob/master/test/WebSites/VirtualDirectory/Startup.cs
Thanks very much - that fixed it for localhost but for local host only. Apologies for not seeing that in the samples. However it still doesn't work when using the server name or fqdn of the server. It just shows a green bar with a blank box. This is the code I have at the moment (it wouldn't work at all without setting the swaggerDoc.Host):
app.UseSwagger((httpRequest, swaggerDoc) =>
{
swaggerDoc.Host = httpRequest.Host.Value;
});
app.UseSwaggerUi(swaggerUrl: Configuration["AppSettings:VirtualDirectory"] + "/swagger/v1/swagger.json");
I appreciate your help!
For anyone interested, the issue with using the server name in the url went away when I turned off IE compatibility mode.
This is still an issue and should probably be fixed. Sounds like of the two arguments that app.UseSwaggerUi(...) takes, the first (baseRoute) one is assumed to be relative to the application (virtual directory) while the second (swaggerUrl) is assumed to be relative to the Website root.
This means that the documentation (from the project's main page) is incorrect:
Once this is done, you should be able to spin up you app and browse the following Swagger JSON and UI endpoints respectively:
your-root-url/swagger/v1/swagger.json
your-root-url/swagger/ui
When the application is hosted in a virtual directory, application root is different than the web root.
This is also the case when using web listener
Can you please help us, how you resolved this issue
If anyone is still having an issue with this I can share my working code...
I'm having this problem too @IanElsinga
Can you share your working code where you fixed the issue @IanElsinga
Apologies for the delay - here is all my swagger-related code from startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
services.AddSwaggerGen();
services.ConfigureSwaggerGen(x =>
{
//x.DocumentFilter
x.SingleApiVersion(new Info
{
Description = "Description goes here",
Version = "v1.5",
Title = "Api " + apiVersion
});
x.IncludeXmlComments(AppContext.BaseDirectory + "YourProject.Api.xml");
x.IgnoreObsoleteProperties();
x.IgnoreObsoleteActions();
x.DescribeAllEnumsAsStrings();
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
app.UseSwagger((httpRequest, swaggerDoc) =>
{
swaggerDoc.Host = httpRequest.Host.Value;
});
app.UseSwaggerUi(swaggerUrl: Configuration["AppSettings:VirtualDirectory"] + "/swagger/" + apiVersion + "/swagger.json");
}
md5-d4a2d05ee1de5552a76b19ec5a58a621
"AppSettings": {
"VirtualDirectory": "/Api"
}
Does that resolve the issue?
@IanElsinga Has anyone solved this? I have the same issue when deploying. I have tried every combination I can think of, with your answer and more.
I also tried this but it is not working, Looking for some solution
@IanElsinga, @sachmahajan - if you're still having issues you may want to check out the solution described in #171. In a nutshell, you provide Swagger JSON endpoints that are relative tho the swagger-ui page so you're configuration can be agnostic of virtual directories.
Closing as this is essentially a dup of #171
This fixed the issue for me. In Startup.cs I have added
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Service API V1");
});
Showing the service-methods locally is working out of the box. After deploy to the server I have edit the url to the swagger.json file and set: https://testservices.xyz.no/dev/myService/swagger/v1/swagger.json instead of the default: http://localhost:2345/swagger/v1/swagger.json
Thanks for the tips everyone. Helped me figure out this issue quickly.
I had the same issue and did the following to use IIS Express when debugging, and IIS when deployed:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Enable middle ware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
#if DEBUG
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Jwt Security Api v1 (DEBUG)");
#else
c.SwaggerEndpoint("/[]virtualDir]/swagger/v1/swagger.json", "Jwt Security Api v1 (RELEASE)");
#endif
});
Thanks @timhodgson. That worked for my project.
Still anybody find solution for this issue

My StartUp file is below
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "ML API", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "ML API V1");
c.RoutePrefix = string.Empty;
});
app.UseHttpsRedirection();
app.UseMvc();
}
}
But I am getting this file once I navigate through URL

Most helpful comment
Can you please help us, how you resolved this issue