c# - 如何返回带有错误消息或异常的 NotFound() IHttpActionResult?

标签 c# asp.net-web-api http-status-code-404 httpresponse

当在我的 WebApi GET 操作中找不到某些内容时,我将返回一个 NotFound IHttpActionResult。除了此响应,我还想发送自定义消息和/或异常消息(如果有)。当前 ApiControllerNotFound() 方法不提供传递消息的重载。

有什么办法吗?或者我必须编写自己的自定义 IHttpActionResult

最佳答案

这是一个用简单消息返回 IHttpActionResult NotFound 的代码:

return Content(HttpStatusCode.NotFound, "Foo does not exist.");

关于c# - 如何返回带有错误消息或异常的 NotFound() IHttpActionResult?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139621/

相关文章:

Java EE - HTTP 错误 404 - 资源不可用 - 在页面上点击 "Submit"按钮后

c# - 我怎样才能使矩形可点击,C#

c# - 在 Unity 进程和另一个 C# 进程之间执行本地 IPC 的最快方法

python - 即使使用 --insecure 后 Django 图像也不会加载

ruby-on-rails - 在 Rails 3 中为自定义 404 捕获未知操作

c# - ASP.NET Web API 的 CaSTLe Windsor ApiController Factory 实现

c# - 如何阻止选择标签助手自动添加 'multiple' 属性

c# - 从注册表中的 DWORD 中获取十进制值

asp.net-web-api - 最大扩展深度,带有最新的Web API和 Breeze

asp.net - SignalR 不再工作 "No assembly found containing an OwinStartupAttribute"