-
By default, our api has no annotations
-
Add xml comments above the api method
-
Configure Swagger in the Program.cs file, add the following code
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo {
Title = "My API", Version = "v1" });
var xmlFile = $"{
Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
- Start the project and see the effect
error problem
If the following error is reported at startup, it is because Swagger needs to access the XML comment file, but the file is not generated in your project.
In order to generate this file, follow these steps:
- Right click on the project in Visual Studio and select Properties.
- Select the Generate tab. In the "Output" drop-down menu, tick "Generate a file containing API documentation".
- Save changes and rebuild the project.
After rebuilding the project, the XML annotation files should be generated so that Swagger can access them.