如何返回带有错误消息或异常的 NotFound() IHttpActionResult? [英] How do I return NotFound() IHttpActionResult with an error message or exception?
问题描述
当在我的 WebApi GET 操作中找不到某些内容时,我将返回 NotFound IHttpActionResult
.连同此响应,我想发送自定义消息和/或异常消息(如果有).当前 ApiController
的 NotFound()
方法不提供传递消息的重载.
I am returning a NotFound IHttpActionResult
, when something is not found in my WebApi GET action. Along with this response, I want to send a custom message and/or the exception message (if any). The current ApiController
's NotFound()
method does not provide an overload to pass a message.
有没有办法做到这一点?或者我必须编写自己的自定义 IHttpActionResult
?
Is there any way of doing this? or I will have to write my own custom IHttpActionResult
?
推荐答案
这是一个单行代码,用于返回 IHttpActionResult NotFound 并带有一条简单的消息:
Here's a one-liner for returning a IHttpActionResult NotFound with a simple message:
return Content(HttpStatusCode.NotFound, "Foo does not exist.");
这篇关于如何返回带有错误消息或异常的 NotFound() IHttpActionResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!