I am using the following Nuget in my ASP.NET Web API: https://www.nuget.org/packages/Swashbuckle/ Version: 5.6.0
I see the base URL on the Swagger for my API, shows blank. Is there a way to add base URL or remove that tag entirely?
Click here to see the image of the base URL on the swagger
I installed the Nuget package & updated the generated SwaggerConfig.cs as follows:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "API title").Contact(cc => cc.Name("team name").Email("email address"));
c.IncludeXmlComments(string.Format(#"{0}\bin\xmlName.XML", System.AppDomain.CurrentDomain.BaseDirectory));
})
.EnableSwaggerUi(c =>
{
c.DocumentTitle("document title");
c.DisableValidator();
c.DocExpansion(DocExpansion.List);
});
Related
I have a blazor wasm application running on a subpath Apps/Genesis/
The problem is some DLL's are not being found:
The request is:
https://localhost:5001/Apps/Genesis/Apps/Genesis/_framework/Blazored.LocalStorage.dll
The request should be:
https://localhost:5001/Apps/Genesis/_framework/Blazored.LocalStorage.dll
app.MapWhen(
context =>
{
return context.Request.Path.StartsWithSegments("/Apps/Genesis");
},
genesis =>
{
// Define blazor files
genesis.UseBlazorFrameworkFiles("/Apps/Genesis");
// Access static files (Ex.: images, dll's) - Used twice because it's on the microsoft docs example
genesis.UseStaticFiles();
genesis.UseStaticFiles("/Apps/Genesis");
// Apply routing for UseEndpoints
genesis.UseRouting();
// Especify Routes
genesis.UseEndpoints(
endpoints =>
{
endpoints.MapControllers();
endpoints.MapFallbackToFile("Apps/Genesis/{{*path:nonfile}}", "Apps/Genesis/index.html");
});
});
Having the below code to configure in Startup.cs
Variables.
private const string SwaggerDocumentVersionName = "v1";
private static string SwaggerDocumentServiceName => $"Users API({SwaggerDocumentVersionName})";
ConfigureServices method.
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(
SwaggerDocumentVersionName,
new OpenApiInfo
{
Title = SwaggerDocumentServiceName,
Version = $"{SwaggerDocumentVersionName}"
});
});
Configure method.
app.UseSwagger(c =>
{
c.RouteTemplate = "/swagger/{documentName}/swagger.json";
});
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "swagger/ui";
c.SwaggerEndpoint($"/swagger/{SwaggerDocumentVersionName}/swagger.json", SwaggerDocumentServiceName);
});
When running locally (https://localhost:5001/swagger/ui resolved to https://localhost:5001/swagger/ui/index.html) definition is loaded correctly and everything seems fine.
Deploying the service to AWS Lambda as ASP.NET Core REST API and navigating to the URL (https://DNS_URL/API_PREFIX/swagger/ui resolved to https://DNS_URL/API_PREFIX/swagger/ui/index.html)it shows the below error loading the JSON definition.
The interesting part is that if you navigate to the JSON definition route (https://DNS_URL/API_PREFIX/swagger/v1/swagger.json) it shows the definition.
The main URL for the API you have released on lambda is https://DNS_URL/API_PREFIX/
Swagger UI needs to fetch the swagger.json file in order for it to work, and for your localhost it is working correctly since https://localhost:5001/swagger/v1/swagger.json is a valid endpoint
(*you have no prefix here)
And the version deployed to lambda is trying to fetch this swagger.json file under
https://DNS_URL/swagger/v1/swagger.json - without your API_PREFIX, thus it's returning 404, not found and swagger ui is displaying the Error message.
Quick fix, which you might apply, that I think would work:
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "swagger/ui";
c.SwaggerEndpoint($"{env.IsDevelopment() ? "" : API_PREFIX}/swagger/{SwaggerDocumentVersionName}/swagger.json", SwaggerDocumentServiceName);
});
Where the API_PREFIX is a string starting with '/'
i'm using swagger ui in my asp.net mvc
Swashbuckle v 5.6
asp.net v 4.8
on local machine everythink working successfully but unfortunately after deploy website to cloud, swagger not load static content, but wen i navigate to these resources using chrome it's loadded successfully
i config swagger using:
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.Schemes(new[] { "http", "https" });
c.IgnoreObsoleteActions();
c.SingleApiVersion("v1", "Console - API");
c.ApiKey("auth")
.Description("API Key Authentication")
.Name("auth")
.In("header");
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
c.IncludeXmlComments(String.Format(#"{0}\bin\docs.xml", System.AppDomain.CurrentDomain.BaseDirectory));
c.UseFullTypeNameInSchemaIds();
})
.EnableSwaggerUi(c =>
{
c.DocumentTitle("Console - API");
c.EnableApiKeySupport("auth", "header");
c.InjectJavaScript(thisAssembly, "Console.API.Assets.Swagger.SwashbuckleCustomAuth.js");
c.InjectStylesheet(thisAssembly, "Console.API.Assets.Swagger.SwashbuckleCustomStyle.css");
});
}
Do i miss any thing? dose https effects swagger load static files?
pss: i don't think this issue related to iis because i have placed a static file in root folder of the website and i was able to load it
The IIS was block simultaneous requests, and the solution was by increment maximum concurrent connections in site advance settings in IIS
I've created one API project with asp.net core in which I used swashbuckle.AspNetCore.dll to generate swagger.
It is working perfectly and going well.
Now I have another C#(.NET Standard library) in which I have created some GET, POST, DELETE, PUT method.
Need to load all of those methods to the swagger API project.
I've tried to use that separate C# library(.dll) to my project, but it didn't load to swagger.
Added this line to configuration service method at Startup.cs file:
services.AddSwaggerGen(c => {
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
c.AddSecurityDefinition("Bearer", new ApiKeyScheme {
In = "header", Description = "Please enter JWT with Bearer into field",
Name = "Authorization", Type = "apiKey"
});
c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> {
{ "Bearer", Enumerable.Empty<string>() },
});
});
Added this line to Configure method at startup.cs file:
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});
It shows swagger with swagger project method. It should show both methods whether it is in a separate library or swagger project itself.
I used service stack swagger instead of swashbuckle.
It's solve my problem.
here is the solution.
https://forums.servicestack.net/t/guidance-on-enabling-oauth-2-0-in-generated-swagger-docs/3995/18
I am trying to add swagger to my service, but I am having some problems.
When I go to my page I get multiple calls to my webpage
/swagger/index.html -> Returns 200
/swagger/swagger-ui.css -> Returns 500
/swagger/swagger-ui-bundle.js -> Returns 500
/swagger/swagger-ui-standalone-preset.js -> Returns 500
/swagger/favicon-32x32.png -> Returns 500
/swagger/favicon-16x16.png -> Returns 500
If I go to localhost/api/myServiceName/swagger/v1/swagger.json, the file looks ok. I can see my endpoints and DTOs.
In my code I do the following:
ConfigureServices():
services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Info { Title = "MyService API", Version = "v1" }); });
Configure():
app.UseSwagger();
app.UseSwaggerUI(
c =>
{
c.SwaggerEndpoint($"/api/MyService/swagger/v1/swagger.json",
"MyService API V1");
});
My project references:
Swashbuckle.AspNetCore 3.0.0
Swashbuckle.AspNetCore.Swagger 3.0.0
Swashbuckle.AspNetCore.SwaggerGen 3.0.0
Swashbuckle.AspNetCore.SwaggerUI 3.0.0
I am pretty sure something is wrong with my configuration. Any idea on what the error is?
Add a reference to Microsoft.AspNetCore.StaticFiles
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/851