问题
Swashbuckle would not generate swagger.json with an output of "UserCreateResponse", how do you fix this?
[HttpPost]
public async Task<IActionResult> Update([FromBody]UserCreate Request)
{
UserCreateResponse response = new UserCreateResponse();
//do something here
// returns UserCreateResponse with http status code 200
return Ok(response);
}
You can't do this, because its not going to return the http status code, 200,400,401 etc
[HttpPost]
public async Task<UserCreateResponse> Update([FromBody]UserCreate Request)
{
UserCreateResponse response = new UserCreateResponse();
//do something here
// returns UserCreateResponse
return response;
}
回答1:
You can specify the response type with the following attribute:
[ProducesResponseType(typeof(UserCreateResponse), 200)]
回答2:
The below solution works only for Swashbuckle versions prior to V6.0!
From V6.0 onwards SwaggerResponse
isn't supported anymore, see here.
Another variant is the use of the SwaggerResponse
attribute, which also allows to provide an additional description:
[SwaggerResponse(HttpStatusCode.OK, "UserDTO", typeof(UserDTO))]
public async Task<IHttpActionResult> Get([FromODataUri] int key)
{
var result = await UserRepo.GetAsync(key);
...
return Ok(result);
}
which produces output as shown here:
It's also possible to omit the type to document other status codes which do not return an entity:
[SwaggerResponse(HttpStatusCode.NotFound, "no data found")]
[SwaggerResponse(HttpStatusCode.BadRequest, "requiered request headers not found")]
来源:https://stackoverflow.com/questions/45182448/swashbuckle-swagger-documentation-of-returned-response