我装饰了一个 Action 如下
[SwaggerResponse(HttpStatusCode.OK, "List of customers", typeof(List<CustomerDto>))]
[SwaggerResponse(HttpStatusCode.NotFound, Type = typeof(NotFoundException))]
OK 模型正确显示。
但是,在 Repsonse 消息下,对于 NotFound
,我得到“Object is not a primitive”。自定义异常派生自 Exception
,实现了 ISerializable
并且还具有 [Serializable]
和 [DataContract()]
如何显示实际数据类型而不是消息?
此外,如果我使用此类属性装饰所有操作,在正常使用 WebApi 时是否会导致性能下降?
最佳答案
怎么样:
[SwaggerResponse(HttpStatusCode.OK, "List of customers", typeof(IEnumerable<int>))]
[SwaggerResponse(HttpStatusCode.BadRequest, Type = typeof(BadRequestErrorMessageResult))]
[SwaggerResponse(HttpStatusCode.NotFound, Type = typeof(NotFoundResult))]
public IHttpActionResult GetById(int id)
{
if (id > 0)
return Ok(new int[] { 1, 2 });
else if (id == 0)
return NotFound();
else
return BadRequest("id must be greater than 0");
}
关于c# - 对象不是响应消息模型的原语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41908889/