湖上湖
3种不同的使用选择在所有应用程序控制器中:public void Configure(IApplicationBuilder app, IWebHostEnvironment env){ app.UseSwaggerResponseCheck(); //...}使用 ValidateStatusCodes 属性的每个控制器操作:[ApiController][Route("[controller]")]public class ExampleController : ControllerBase{ [HttpGet] [ValidateStatusCodes] // <-- Use this [SwaggerOperation("LoginUser")] [SwaggerResponse(statusCode: StatusCodes.Status200OK, type: null, description: "signed user email account")] [SwaggerResponse(statusCode: StatusCodes.Status400BadRequest, type: null, description: "wrong email or password")] [Route("/users/login")] public virtual IActionResult LoginUser([FromQuery][Required()] string email, [FromQuery] string password) { if (email == "email@gmail.com") return Ok("success"); else if (email == "") return BadRequest("email required"); else return NotFound("user not found"); // 500 - InternalServerError because not attributed with SwaggerResponse. } // ... [HttpGet] [ValidateStatusCodes] // <-- Use this [ProducesResponseType(type: typeof(Account), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [Route("/users/login2")] public virtual IActionResult LoginUser2([FromQuery][Required()] string email, [FromQuery] string password) { if (email == "email@gmail.com") return Ok("success").Validate(); else if (email == "") return BadRequest("email required").Validate(); else return NotFound("user not found").Validate(); // Throws error in DEBUG or Development. }}使用 IStatusCodeActionResult.Validate() 的每个结果:[ApiController][Route("[controller]")]public class ExampleController : ControllerBase{ [HttpGet] [SwaggerOperation("LoginUser")] [SwaggerResponse(statusCode: StatusCodes.Status200OK, type: null, description: "signed user email account")] [SwaggerResponse(statusCode: StatusCodes.Status400BadRequest, type: null, description: "wrong email or password")] [Route("/users/login")] public virtual IActionResult LoginUser([FromQuery][Required()] string email, [FromQuery] string password) { if (email == "email@gmail.com") return Ok("success").Validate(); else if (email == "") return BadRequest("email required").Validate(); else if (email == "secret") return Unauthorized("hello"); // Passed, independent of SwaggerResponse attribute. else return NotFound("user not found").Validate(); // 500 - InternalServerError because not attributed with SwaggerResponse. } // ... [HttpGet] [ProducesResponseType(type: typeof(Account), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [Route("/users/login2")] public virtual IActionResult LoginUser2([FromQuery][Required()] string email, [FromQuery] string password) { if (email == "email@gmail.com") return Ok("success").Validate(); else if (email == "") return BadRequest("email required").Validate(); else return NotFound("user not found").Validate(); // Throws error in DEBUG or Development. }}