I have integrated swagger in my ASP.NET Core RC1 application with following NuGet packages.
"Swashbuckle.SwaggerGen": "6.0.0-rc1-final",
"Swashbuckle.SwaggerUi": "6.0.0-rc1-final"
Here is the code for swagger integration.
public void ConfigureServices(IServiceCollection services)
{
....
.....
//*** Configure Swagger Document Information.
services.ConfigureSwaggerDocument(options =>
{
//*** Define incremental API version.
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "TEST APIs",
Description = "Test API Methods",
TermsOfService = "",
});
//*** Assign the API - Swagger helper document.
options.OperationFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlActionComments(helperDocPath));
});
//*** Configure the Swagger schema settings.
services.ConfigureSwaggerSchema(options =>
{
options.DescribeAllEnumsAsStrings = true;
options.ModelFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlTypeComments(helperDocPath));
});
....
....
}
//**** Configure Method
private void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
....
//*** Add Swagger pluggins to application environment.
app.UseSwaggerGen();
app.UseSwaggerUi();
}
The code generates the swagger documentation while accessing it locally using localhost -> "http://localhost:8080/testapiproject/swagger/ui/index.html".
However, after deploying the code in the deployment server i am still getting the swagger document but i am getting "Error" in the bottom, upon clicking it says,
{"schemaValidationMessages":[{"level":"error","message":"Can't read from file http://<applicationdomainname>:8080//testapiproject/swagger/v1/swagger.json"}]}.
I think it is trying to access json
data from a wrong path. Make sure the path is configured correctly in the testapiproject/swagger/ui/index.html"
of swaggerui
$(function() {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "/swagger/docs/v1"; // Make sure it's not hardcoded
}
}
UPDATED for .Net Core
I installed only "Swashbuckle": "6.0.0-beta902"
package.
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen();
services.ConfigureSwaggerGen(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "test API",
Description = "test api for swagger",
TermsOfService = "None",
});
options.IncludeXmlComments(/* whatever tha path */);
options.DescribeAllEnumsAsStrings();
});
}
In the configurre mehod added just
app.UseSwagger();
app.UseSwaggerUi(); // inside this method we can pass the root path of swagger
It was working with localhost but not in vitural directory. So i had to add the below config to web.config then it started working.
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
Swager generates API docs from code comments that store in xml files. For example, comments for GatewayServer.dll will store in GatewayServer.xml. You need to upload its on deployment server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With