我如何用一个错误信息或异常返回NOTFOUND()IHttpActionResult? [英] How do I return NotFound() IHttpActionResult with an error message or exception?

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

问题描述

我返回NOTFOUND IHttpActionResult ,当自己是不是在我的WebAPI GET行动中。随着这一反应,我想发送自定义邮件和/或异常消息(如有)。当前 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?

推荐答案

您就需要编写自己的行动的结果,如果你想定制响应消息的形状。

You'd need to write your own action result if you want to customize the response message shape.

我们希望提供最常见的响应消息塑造出来的东西像简单的空404的方块,但我们也想保持这些成果尽可能简单;使用操作结果的主要优点之一是,它使你的操作方法很多单元测试更容易。我们把行动的结果更多的属性,更多的东西你的单元测试需要考虑,以确保操作方法是做你所期望的东西。

We wanted to provide the most common response message shapes out of the box for things like simple empty 404s, but we also wanted to keep these results as simple as possible; one of the main advantages of using action results is that it makes your action method much easier to unit test. The more properties we put on action results, the more things your unit test needs to consider to make sure the action method is doing what you'd expect.

我经常想提供一个自定义的消息一样,所以随时登录错误为我们考虑支持在将来的版本这一行动结果的能力:
<一href=\"https://aspnetwebstack.$c$cplex.com/workitem/list/advanced\">https://aspnetwebstack.$c$cplex.com/workitem/list/advanced

I often want the ability to provide a custom message as well, so feel free to log a bug for us to consider supporting that action result in a future release: https://aspnetwebstack.codeplex.com/workitem/list/advanced

有关操作结果的一个好处,虽然是,如果你想要做一些稍微不同的你总是可以编写自己还算方便。这里是你会如何做,在你的情况(假设你想在text / plain的错误信息;如果你想JSON,你会做一些与内容略有不同):

One nice thing about action results, though, is that you can always write your own fairly easily if you want to do something slightly different. Here's how you might do it in your case (assuming you want the error message in text/plain; if you want JSON, you'd do something slightly different with the content):

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);
    }
}

然后,在你的操作方法,你可以做这样的事情:

Then, in your action method, you can just do something like this:

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

如果您使用了自定义控制器基类(而不是从ApiController直接继承),你也可以消除这一点。部分(调用扩展方法时,这是不幸的是必填项):

If you used a custom controller base class (instead of directly inheriting from ApiController), you could also eliminate the "this." part (which is unfortunately required when calling an extension method):

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.");
    }
}

这篇关于我如何用一个错误信息或异常返回NOTFOUND()IHttpActionResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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