How to show c# validation attributes on query parameters in Swagger
我将 Swagger 与 ASP.Net Core 2.1 Web API 项目一起使用。这是一个示例控制器操作方法:
1 2 3 | [HttpGet] public string GetString([Required, MaxLength(20)] string name) => $"Hi there, {name}."; |
这就是我在 Swagger 文档中得到的内容。如您所见,Swagger 显示了
如果我在作为 POST 操作方法的参数的 DTO 类上使用
如何让 Swagger 显示查询参数的
注意:我尝试将
在 .NET Core 中,您可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public static IApplicationBuilder UseR6SwaggerDocumentationUI( this IApplicationBuilder app) { app.UseSwagger(); app.UseSwaggerUI(c => { //Allow to add addition attribute info on doc. like [MaxLength(50)] c.ConfigObject = new ConfigObject { ShowCommonExtensions = true }; c.SwaggerEndpoint("/swagger/v1/swagger.json","Asptricks.net API"); c.RoutePrefix ="api_documentation/index"; c.InjectStylesheet("/swagger-ui/custom.css"); c.InjectJavascript("/swagger-ui/custom.js"); c.SupportedSubmitMethods( new[] { SubmitMethod.Patch }); //Collapse model near example. c.DefaultModelExpandDepth(0); //Remove separate model definition. c.DefaultModelsExpandDepth(-1); }); return app; } |