I am using asp.net core 3.1 with swashbuckle 5. I am not able tofind the way for defining base url.
RootUrl option seem to be not coming or (may be i am not able to call it)
Please let me know how to define it
private static void SwaggerInjection(IServiceCollection services)
{
services.AddSwaggerGen(x =>
{
x.SwaggerDoc("v1", new OpenApiInfo { Title = "Portal Backend APIs", Version = "v1" });
x.DescribeAllEnumsAsStrings();
x.TagActionsBy(api =>
{
if (api.GroupName != null)
{
return new[] { api.GroupName };
}
var controllerActionDescriptor = api.ActionDescriptor as ControllerActionDescriptor;
if (controllerActionDescriptor != null)
{
return new[] { controllerActionDescriptor.ControllerName };
}
throw new InvalidOperationException("Unable to determine tag for endpoint.");
});
x.DocInclusionPredicate((name, api) => true);
x.AddSecurityDefinition("Bearer", // name the security scheme
new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme.",
Type = SecuritySchemeType.Http, // we set the scheme type to http since we're using bearer authentication
Scheme = "bearer" // the name of the HTTP Authorization scheme to be used in the Authorization header. In this case "bearer".
});
x.AddSecurityRequirement(new OpenApiSecurityRequirement{
{
new OpenApiSecurityScheme{
Reference = new OpenApiReference{
Id = "Bearer", // the name of the previously defined security scheme.
Type = ReferenceType.SecurityScheme
}
},new List<string>()
}
});
// to show API description and other stuff on swagger
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
x.IncludeXmlComments(xmlPath);
});
}
I'm not sure what you mean - could you please elaborate on your setup and what you're actually trying to do.
i have my application deployed on some linux server. private ip url http://xxx.xxx.xxx.xxx:4000/swagger/index.html is serving fine.
I have deployed same url under with ngnix with url like http://mywebsitebasepath/myvitualpath/swagger/index.html but api url is missing myvitualpath and api address is returning 404 error.
Any setting i also needed to do?
https://github.com/domaindrivendev/Swashbuckle.AspNetCore#dealing-with-proxies-that-change-the-request-path
Changed My function in startup.cs from
private void SwaggerSetup(IApplicationBuilder app)
{
var swaggerOptions = new SwaggerOptions();
Configuration.GetSection(nameof(SwaggerOptions)).Bind(swaggerOptions);
app.UseSwagger(option => { option.RouteTemplate = swaggerOptions.JsonRoute; });
app.UseSwaggerUI(option =>
{
option.SwaggerEndpoint(swaggerOptions.UIEndpoint, swaggerOptions.Description);
});
}
to
private void SwaggerSetup(IApplicationBuilder app)
{
////https://github.com/domaindrivendev/Swashbuckle.AspNetCore#dealing-with-proxies-that-change-the-request-path
app.Use((context, next) =>
{
if (context.Request.Headers.TryGetValue("X-Forwarded-Prefix", out var value))
context.Request.PathBase = value.First();
return next();
});
var swaggerOptions = new SwaggerOptions();
Configuration.GetSection(nameof(SwaggerOptions)).Bind(swaggerOptions);
app.UseSwagger(option => { option.RouteTemplate = swaggerOptions.JsonRoute; });
app.UseSwaggerUI(option =>
{
option.SwaggerEndpoint("v1/swagger.json", swaggerOptions.Description);
});
}
but not effect
I also experience the same issue: the FQDN for the API server is: productname-api.domain-name.com, and we have a web route in front of it. So, people will need to use productname.domain-name.com/api to access the APIs. However, when swagger is displayed in the browser, it uses the original FQDN (productname-api.domain-name.com) instead of productname.domain-name.com/api. Therefore, whenever users click on the Try It button, it fails because it's unable to resolve.
The hostname listed in the host drop-down list is the original FQDN instead of the /api ones.
I've read the https://github.com/domaindrivendev/Swashbuckle.AspNetCore#dealing-with-proxies-that-change-the-request-path, and it doesn't resolve my issue.
I've also using v6.0.7 now and still having the same issue.
Any suggestions on how to solve it? Or should I create a pull request?
Thank you.
The URL that you pass to SwaggerEndpoint is what the swagger-ui, a client-side application, uses to retrieve your API metadata. When the leading slash is omitted, you're telling the swagger-ui that the path is relative to _itself_. So, assuming the default RoutePrefix = "swagger" for the SwaggerUI middleware, you're telling it that the Swagger endpoint is available at swagger/v1/swagger.json. While this would be correct with the default setup, it won't work in your case because you've also changed the RouteTemplate for the Swagger middleware. To account for this, you need to alter the path provided to SwaggerEndpoint accordingly. For example, something along the following lines should work:
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"../{swaggerOptions.JsonRoute}".Replace("{documentName}", "v1"), "V1 Docs");
});
Hi, I think I need to clarify.
The swagger.json can be found in its current location; it's never the issue.
I'm trying to find out how to modify the hostnames listed in the Server dropdown list in the swagger.json.
I need to customise the Server dropdown list's content; at the moment, swashbuckler automatically uses the server's fqdn (productname-api.domain-name.com) instead of its accessible fqdn (productname.domain-name.com/api). I can't seem to find a way to do that.
If that's what you've been trying to tell me, then I apologise as I've certainly missed your explanation.
Looking forward to your further enlightenment.
The URL that you pass to
SwaggerEndpointis what the swagger-ui, a client-side application, uses to retrieve your API metadata. When the leading slash is omitted, you're telling the swagger-ui that the path is relative to _itself_. So, assuming the defaultRoutePrefix = "swagger"for the SwaggerUI middleware, you're telling it that the Swagger endpoint is available atswagger/v1/swagger.json. While this would be correct with the default setup, it won't work in your case because you've also changed theRouteTemplatefor the Swagger middleware. To account for this, you need to alter the path provided toSwaggerEndpointaccordingly. For example, something along the following lines should work:app.UseSwaggerUI(c => { c.SwaggerEndpoint($"../{swaggerOptions.JsonRoute}".Replace("{documentName}", "v1"), "V1 Docs"); });
not worked
@KamranShahid, @alexkusuma - could you try pulling down the latest version (6.1.0) to see if this works for you?
In 6.0.0, I introduced logic to _automatically_ infer and populate the servers metadata using info from the current request (Host header etc.), which drives the server dropdown in the swagger-ui. In doing this, I knew it would be problematic for apps that are behind a proxy, and provided guidance to use Microsoft's Forwarded Headers middleware, as this is their _recommended_ solution for apps behind a proxy.
In hindsight though, I think this has caused more problems than it solved, and so as of 6.1.0, I've reverted to the old behavior that simply leaves the servers section empty. In this case, the swagger-ui will infer the absolute URL based on the current browser location (the fqdn). I think this should work for your case - can you try it and let me know?
Hi @domaindrivendev - I had just upgraded it to 6.1.0 will test it in the next few days or early next week. I need to focus on the other part of the project for now. I will let you posted.
Hi @domaindrivendev - I had just upgraded it to 6.1.0 will test it in the next few days or early next week. I need to focus on the other part of the project for now. I will let you posted.
Have you able to resolved it?
Hi @domaindrivendev and @KamranShahid,
Unfortunately, it's not resolved yet. I've tried it with 2 servers, both behind webroute. the swagger kept using fqdn for the URL instead of the one that's accessible for user. For illustration:
server's fqdn is: servername-api.domain-name.com/
fqdn that's accessible from user is: service-name.domain-name.com/api
so on swagger page, when I have the the definition, let say for API called: api-endpoint-1
when I click on try it button, swagger kept using: servername-api.domain-name.com/api-endpoint-1
while actually it supposed to use: service-name.domain-name.com/api/api-endpoint-1
Hi @domaindrivendev, should I do a pull request to solve this?
@alexkusuma - if you've upgraded to 6.1.0 and are still seeing issues, this is most likely a config issue. Could you possibly create a minimal app (i.e. starting with blank project) that repro's the issue and post to github so I can pull down and troubleshoot? I can use docker to put a proxy in front of it to mimic the setup you've described.