Swagger增加注释其实很简单
右键项目,属性勾选文档文件即可
然后代码配置路径:
ApiVersions是版本,可以忽略并写死
services.AddSwaggerGen(option => { foreach (var version in typeof(ApiVersions).GetEnumNames()) { option.SwaggerDoc(version, new OpenApiInfo() { Title = !string.IsNullOrWhiteSpace(docName) ? docName : $"Swagger的Api文档", Version = version, Description = !string.IsNullOrWhiteSpace(docDescription) ? docDescription : $"通用版本的CoreApi版本v1" }); } // xml文档绝对路径 var file = Path.Combine(AppContext.BaseDirectory, $"{AppDomain.CurrentDomain.FriendlyName}.xml"); // true : 显示控制器层注释 option.IncludeXmlComments(file, true); // 对action的名称进行排序,如果有多个,就可以看见效果了。 option.OrderActionsBy(o => o.RelativePath); });
在UseSwagger中配置下:
app.UseSwagger(); app.UseSwaggerUI(option => { foreach (string version in typeof(ApiVersions).GetEnumNames()) { option.SwaggerEndpoint($"/swagger/{version}/swagger.json", string.IsNullOrWhiteSpace(docName) ? docName : $"Steven文档【{version}】版本"); } });
这样就可以了
标签:option,注释,docName,version,ApiVersions,Net7,Swagger,string From: https://www.cnblogs.com/SevenWang/p/17628611.html