如何返回带有错误消息或异常的 NotFound() IHttpActionResult? [英] How do I return NotFound() IHttpActionResult with an error message or exception?

查看:23
本文介绍了如何返回带有错误消息或异常的 NotFound() IHttpActionResult?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆