{ new OpenApiServer { Url = $\"{basePath}\" } }); });app.UseSwaggerUI(c => {
c.SwaggerEndpoint($\"{ basePath}/swagger/v1/swagger.json\ });
return app;}
多版本⽀持
services.AddSwaggerGen(c =>{
c.SwaggerDoc(\"v1\ c.SwaggerDoc(\"v2\})
[HttpPost]
[ApiExplorerSettings(GroupName = \"v2\")]public void Post([FromBody]Product product)更完善的枚举⽀持
Install-Package Unchase.Swashbuckle.AspNetCore.Extensionsservices.AddSwaggerGen(options => { //...
// or configured:
options.AddEnumsWithValuesFixFilters(services, o => {
// add schema filter to fix enums (add 'x-enumNames' for NSwag) in schema o.ApplySchemaFilter = true;
// add parameter filter to fix enums (add 'x-enumNames' for NSwag) in schema parameters o.ApplyParameterFilter = true;
// add document filter to fix enums displaying in swagger document o.ApplyDocumentFilter = true;
// add descriptions from DescriptionAttribute or xml-comments to fix enums (add 'x-enumDescriptions' for schema extensions) for applied filters o.IncludeDescriptions = true;
// add remarks for descriptions from xml-comments o.IncludeXEnumRemarks = true;
// get descriptions from DescriptionAttribute then from xml-comments
o.DescriptionSource = DescriptionSources.DescriptionAttributesThenXmlComments;
// get descriptions from xml-file comments on the specified path // should use \"options.IncludeXmlComments(xmlFilePath);\" before o.IncludeXmlCommentsFrom(xmlFilePath); // the same for another xml-files... }); });
枚举⽂档效果
OAuth2.0⽀持
Install-Package Swashbuckle.AspNetCore.Filters⼿填AccessToken(apikey)⽅式 services.AddSwaggerGen(c => {
//...
c.OperationFilter();c.AddSecurityDefinition(\"oauth2\ {
Description = \"Standard Authorization header using the Bearer scheme. Example: \\\"bearer {token}\\\"\ In = ParameterLocation.Header, Name = \"Authorization\
Type = SecuritySchemeType.ApiKey }); });效果
引导跳转OAuth服务器⽅式
services.AddSwaggerGen(c => {
//...
c.OperationFilter();c.AddSecurityDefinition(\"oauth2\ {
Type = SecuritySchemeType.OAuth2, Flows = new OpenApiOAuthFlows() {
Implicit = new OpenApiOAuthFlow() {
AuthorizationUrl = new Uri(\"https://your.identityserver.io/connect/authorize\"), Scopes = new Dictionary {{ \"testapi.rw\授权访问测试TestApi\" } } } } });
});
app.UseSwaggerUI(c =>{
//...
c.OAuthClientId(\"test_swaager\");});效果
忽略某个Api[HttpGet(\"{id}\")]
[ApiExplorerSettings(IgnoreApi = true)]public Product GetById(int id)修改传输数据类型
services.AddSwaggerGen(c => {
//long类型转string
c.MapType(() => new OpenApiSchema { Type = \"string\" }); });⾃定义描述(标签)
Install-Package Swashbuckle.AspNetCore.Annotations[SwaggerTag(\"Create, read, update and delete Products\")]public class ProductsController{ ...}
Quer请求参数⼩驼峰
public class QueryBillDto {
/// /// PID/⼿机号/昵称 ///
public string Query { get; set; } /// //////
public EApp? Appid { get; set; }
/// /// 收⼊/⽀出类型 ///
public EnumBillType? BillType { get; set; } /// /// 账单起始⽇期 ///
public DateTime? BillBegin { get; set; }
/// /// 账单截⽌⽇期 ///
public DateTime? BillEnd { get; set; } } ...
//⽐如定义了这样的接⼝
public async Task GetBillList([FromQuery] QueryBillDto dto) {return Ok(); }
swagger-ui需要显⽰ 为⼩写的这样: services.AddSwaggerGen(c => {
//参数描述⼩驼峰
o.DescribeAllParametersInCamelCase(); });