Generating a Swagger file from the ASP.NET Core application

To generate the Swagger file automatically in your project, I recommend using the popular Swashbuckle (https://github.com/domaindrivendev/Swashbuckle.AspNetCore) NuGet package.

Swashbuckle uses the metadata of your API to build the OpenAPI file. The metadata is automatically created by ASP.NET Core, based on the routing attributes, and the method's signature of the action in your controllers.

To  use Swashbuckle, do the following:

  1. Install the Swashbuckle.AspNetCore NuGet package.
  2. Register the Swagger generator in the ConfigureServices method of your Startup class. In the registration process, specify the details of the API and its Version (note that you can define more than one Swagger document):
    services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "GiveNTake.API", Version
= "v1" });
});
  1. Enable the Swagger middleware by adding the following line in the Configure method of your Startup class:
    public void Configure(IApplicationBuilder app, IHostingEnvironment 
env)
{
...
app.UseSwagger();
}
 At this point, the Swagger file will be available when running the application and navigating to http://localhost:[port]/swagger/v1/swagger.json.
The v1 in the URL might be different for you if you set a different version identifier in the Swagger generator registration.
  1. Add the security definition in the Swagger generator registration to make it explicit that the actions in your API require the Authorization header with a Bearer value:
    services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "GiveNTake.API",
Version
= "v1" });
c.AddSecurityDefinition("Bearer", new ApiKeyScheme
{
Name = "Authorization",
In = "header",
});
c.AddSecurityRequirement(new Dictionary<string,
IEnumerable<string>>

{
{ "Bearer", new string[] { } }
});
});
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.156.93