Swashbuckle.AspNetCore如何描述错误响应模型?
Swashbuckle.AspNetCore how to describe error response model?
我有一个 ASP.NET Core v2.1 和 Swashbuckle.AspNetCore
软件包。
我有以下错误响应模型:
public class ErrorResponse
{
[JsonProperty(PropertyName = "error")]
public Error Error { get; set; }
}
public class Error
{
[JsonProperty(PropertyName = "code")]
public string Code { get; set; }
[JsonProperty(PropertyName = "message")]
public string Message { get; set; }
[JsonProperty(PropertyName = "target")]
public string Target { get; set; }
[JsonProperty(PropertyName = "details")]
public List<ErrorDetail> Details { get; set; }
[JsonProperty(PropertyName = "innererror")]
public InnerError InnerError { get; set; }
}
public class ErrorDetail
{
[JsonProperty(PropertyName = "code")]
public string Code { get; set; }
[JsonProperty(PropertyName = "message")]
public string Message { get; set; }
[JsonProperty(PropertyName = "target")]
public string Target { get; set; }
}
public class InnerError
{
[JsonProperty(PropertyName = "code")]
public string Code { get; set; }
[JsonProperty(PropertyName = "innererror")]
public InnerError NestedInnerError { get; set; }
}
因此,例如,如果出现问题,我的 API 端点 returns 类型 ErrorResponse
对象具有适当的 StatusCode:
if (String.IsNullOrWhiteSpace(token))
{
ErrorResponse errorResponse = new ErrorResponse() { Error = new Error() };
errorResponse.Error.Code = "InvalidToken";
errorResponse.Error.Target = "token";
errorResponse.Error.Message = "Token is not specified";
return new BadRequestObjectResult(errorResponse);
}
我如何使用 Swashbuckle.AspNetCore
生成适当的文档,这样,如果出现问题,客户将知道响应的格式?
看看自述文件:
https://github.com/domaindrivendev/Swashbuckle.AspNetCore#explicit-responses
明确回应
如果您需要指定不同的状态代码 and/or 其他响应,或者您的操作 return IActionResult 而不是响应 DTO,您可以使用随 ASP.NET核心。例如...
[HttpPost("{id}")]
[ProducesResponseType(typeof(Product), 200)]
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
[ProducesResponseType(500)]
public IActionResult GetById(int id)
所以在你的情况下你应该添加:
[ProducesResponseType(typeof(ErrorResponse), 400)]
对于那些 return 错误的操作,这里有一些很好的阅读:
https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/conventions
我有一个 ASP.NET Core v2.1 和 Swashbuckle.AspNetCore
软件包。
我有以下错误响应模型:
public class ErrorResponse
{
[JsonProperty(PropertyName = "error")]
public Error Error { get; set; }
}
public class Error
{
[JsonProperty(PropertyName = "code")]
public string Code { get; set; }
[JsonProperty(PropertyName = "message")]
public string Message { get; set; }
[JsonProperty(PropertyName = "target")]
public string Target { get; set; }
[JsonProperty(PropertyName = "details")]
public List<ErrorDetail> Details { get; set; }
[JsonProperty(PropertyName = "innererror")]
public InnerError InnerError { get; set; }
}
public class ErrorDetail
{
[JsonProperty(PropertyName = "code")]
public string Code { get; set; }
[JsonProperty(PropertyName = "message")]
public string Message { get; set; }
[JsonProperty(PropertyName = "target")]
public string Target { get; set; }
}
public class InnerError
{
[JsonProperty(PropertyName = "code")]
public string Code { get; set; }
[JsonProperty(PropertyName = "innererror")]
public InnerError NestedInnerError { get; set; }
}
因此,例如,如果出现问题,我的 API 端点 returns 类型 ErrorResponse
对象具有适当的 StatusCode:
if (String.IsNullOrWhiteSpace(token))
{
ErrorResponse errorResponse = new ErrorResponse() { Error = new Error() };
errorResponse.Error.Code = "InvalidToken";
errorResponse.Error.Target = "token";
errorResponse.Error.Message = "Token is not specified";
return new BadRequestObjectResult(errorResponse);
}
我如何使用 Swashbuckle.AspNetCore
生成适当的文档,这样,如果出现问题,客户将知道响应的格式?
看看自述文件:
https://github.com/domaindrivendev/Swashbuckle.AspNetCore#explicit-responses
明确回应
如果您需要指定不同的状态代码 and/or 其他响应,或者您的操作 return IActionResult 而不是响应 DTO,您可以使用随 ASP.NET核心。例如...
[HttpPost("{id}")]
[ProducesResponseType(typeof(Product), 200)]
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
[ProducesResponseType(500)]
public IActionResult GetById(int id)
所以在你的情况下你应该添加:
[ProducesResponseType(typeof(ErrorResponse), 400)]
对于那些 return 错误的操作,这里有一些很好的阅读:
https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/conventions