我已经想出如何在swashbuckle中创建响应对象元数据:
[Route("X/{Y:Guid}")]
[HttpGet]
[SwaggerResponse(HttpStatusCode.OK, "Bla", typeof(BlaDto))]
public IHttpActionResult GetSomething([FromUri] Guid someGuid)
{
Bla returnObject;
try
{
returnObject = _service.Get(someGuid);
}
catch (DatabaseException databaseException)
{
ModelState.AddModelError("DatabaseException", databaseException.Message);
return BadRequest(ModelState);
}
return Ok(returnObject);
}
我仍然在寻找为400个错误等定义请求对象元数据和元数据的示例。任何指针都将非常感激。
答案 0 :(得分:2)
Swashbuckle会自动检测操作的对象元数据。可以通过添加更多SwaggerResponseAttribute
来指定错误的对象元数据。
以下是一个例子:
[Route("X}")]
[HttpPost]
[SwaggerResponse(HttpStatusCode.Created, "Bla", typeof(BlaDto))]
[SwaggerResponse(HttpStatusCode.Conflict, Type = typeof(ErrorResponse))]
[SwaggerResponse(HttpStatusCode.BadRequest, Type = typeof(ErrorResponse))]
public IHttpActionResult CreateBla(BlaDto bla)
{
BladDto returnObject;
try
{
returnObject = _service.Create(bla);
}
catch (DatabaseException databaseException)
{
var error = new ErrorResponse { Message = databaseException.Message);
return Content(HttpStatusCode.BadRequest, error);
}
catch (SomeOtherException ex)
{
var error = new ErrorResponse { Message = ex.Message);
return Content(HttpStatusCode.Conflict, error);
}
return Ok(returnObject);
}
public class ErrorResponse
{
string Message { get; set; }
}