如何使用错误消息或异常返回NotFound()IHttpActionResult?

阿杰·贾达夫(Ajay Jadhav)

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

有什么办法吗?还是我必须写自己的习惯IHttpActionResult

德马森

如果要自定义响应消息形状,则需要编写自己的操作结果。

我们想为诸如简单的空404之类的东西提供开箱即用的最常见的响应消息形状,但是我们也希望使这些结果尽可能简单。使用动作结果的主要优点之一是它使您的动作方法更容易进行单元测试。我们为操作结果提供的属性越多,单元测试就需要考虑的内容越多,以确保操作方法能够达到预期的效果。

我通常也希望能够提供自定义消息,因此请随时记录一个错误,以供我们考虑在将来的发行版中支持该操作:https : //aspnetwebstack.codeplex.com/workitem/list/advanced

但是,关于动作结果的一件好事是,如果您想做一些稍有不同的事情,则始终可以轻松编写自己的内容。这是在您的情况下的处理方式(假设您希望在文本/纯文本中显示错误消息;如果需要JSON,则对内容进行一些更改):

public class NotFoundTextPlainActionResult : IHttpActionResult
{
    public NotFoundTextPlainActionResult(string message, HttpRequestMessage request)
    {
        if (message == null)
        {
            throw new ArgumentNullException("message");
        }

        if (request == null)
        {
            throw new ArgumentNullException("request");
        }

        Message = message;
        Request = request;
    }

    public string Message { get; private set; }

    public HttpRequestMessage Request { get; private set; }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        return Task.FromResult(Execute());
    }

    public HttpResponseMessage Execute()
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound);
        response.Content = new StringContent(Message); // Put the message in the response body (text/plain content).
        response.RequestMessage = Request;
        return response;
    }
}

public static class ApiControllerExtensions
{
    public static NotFoundTextPlainActionResult NotFound(this ApiController controller, string message)
    {
        return new NotFoundTextPlainActionResult(message, controller.Request);
    }
}

然后,在您的操作方法中,您可以执行以下操作:

public class TestController : ApiController
{
    public IHttpActionResult Get()
    {
        return this.NotFound("These are not the droids you're looking for.");
    }
}

如果使用自定义控制器基类(而不是直接从ApiController继承),则还可以消除“ this”。部分(不幸的是,调用扩展方法时需要):

public class CustomApiController : ApiController
{
    protected NotFoundTextPlainActionResult NotFound(string message)
    {
        return new NotFoundTextPlainActionResult(message, Request);
    }
}

public class TestController : CustomApiController
{
    public IHttpActionResult Get()
    {
        return NotFound("These are not the droids you're looking for.");
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何显示错误消息而不是Java异常?

如何使用错误界面

正确使用错误

如何使用Bottle HTTPError在JSON中返回错误消息?

Web API 2-返回NotFound(); 与使用全局异常处理程序引发异常

使用Web API文档时如何返回NotFound结果

如何使用错误消息中指定的tbspaceid tableid在DB2中查找表和列

使用线程宏的惯用错误/异常处理

如何避免使用错误版本的数据的错误?

Freer-Simple Freer Monads如何使用错误效果统一IO异常处理

如何在python3中使用错误消息和状态代码创建自定义异常

如何使用错误处理配置从Extbase返回适当的404错误

AWS Xray + Lambda异常处理,如何返回错误消息并将Xray跟踪标记为失败

如何使用错误消息验证电子邮件

如何打印errorUserTitle异常的错误消息

Spring对定制的验证消息使用错误的编码

IHttpActionResult方法无法返回NotFound()

使用错误凭据的Django登录返回200而不是401

如何跟踪双指针使用错误

如果使用错误的文件名打开Excel工作簿,则返回一条消息,而不是一条错误

文件使用错误

如何使用错误消息撤消git pull

为 Task<IHttpActionResult> 方法返回 JSON 错误消息

Laravel 无法捕获异常并使用错误消息重定向

Laravel 内部错误页面显示异常消息而不是通用错误消息

如何从异常 try catch 中返回并显示错误消息

Vue 组件已定义但未使用错误消息。我该如何正确使用它?

如何使用错误消息字符串定位方法?

如何在 Java 中使用错误处理/异常处理?