10

我已经看到使用一些新的 IHttpActionResults 表示 OK、NotFound 的示例。我没有看到任何使用Unauthorized().

我现有的代码如下所示:

catch (SecurityException ex)
{
    request.CreateResponse(HttpStatusCode.Unauthorized, ex.Message);
}

我想用这个替换它:

catch (SecurityException ex)
{
    response = Unauthorized();
}

但我没有看到任何过载来传递异常详细信息。

另外,IHttpActionResult返回 500 错误的等价物是什么?

catch (Exception ex)
{
    response = request.CreateErrorResponse(HttpStatusCode.InternalServerError, 
                                           ex.Message);
}
4

3 回答 3

13

根据ApiController的代码,只有两个重载Unauthorized()

/// <summary>
/// Creates an <see cref="UnauthorizedResult"/> (401 Unauthorized) with the specified values.
/// </summary>
/// <param name="challenges">The WWW-Authenticate challenges.</param>
/// <returns>An <see cref="UnauthorizedResult"/> with the specified values.</returns>
protected internal UnauthorizedResult Unauthorized(params AuthenticationHeaderValue[] challenges)
{
    return Unauthorized((IEnumerable<AuthenticationHeaderValue>)challenges);
}

/// <summary>
/// Creates an <see cref="UnauthorizedResult"/> (401 Unauthorized) with the specified values.
/// </summary>
/// <param name="challenges">The WWW-Authenticate challenges.</param>
/// <returns>An <see cref="UnauthorizedResult"/> with the specified values.</returns>
protected internal virtual UnauthorizedResult Unauthorized(IEnumerable<AuthenticationHeaderValue> challenges)
{
    return new UnauthorizedResult(challenges, this);
}

所以看起来你很不走运,除非你想自己做出改变(fork你自己的WebAPI版本或者做一个pull request来尝试把它放到主分支中)。


IHttpActionResult相当于返回 500 错误是这样的:

/// <summary>
/// Creates an <see cref="ExceptionResult"/> (500 Internal Server Error) with the specified exception.
/// </summary>
/// <param name="exception">The exception to include in the error.</param>
/// <returns>An <see cref="ExceptionResult"/> with the specified exception.</returns>
protected internal virtual ExceptionResult InternalServerError(Exception exception)
{
    return new ExceptionResult(exception, this);
}
于 2013-11-12T17:59:46.793 回答
12

您可以在此处阅读有关 Unathorized() 的更多信息, 如何将状态 401 从 WebAPI 返回到 AngularJS 并包含自定义消息? 如果找到另一种方式 - 不抛出异常并使用 HttpResponseMessage 作为控制器的返回类型,你可以这样做:

return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "I am unauthorized IHttpActionResult custom message!"));

更多关于 ResponseMessage() 在这里 http://msdn.microsoft.com/en-us/library/system.web.http.apicontroller.responsemessage%28v=vs.118%29.aspx

于 2014-12-14T19:17:14.817 回答
3

这是我针对带有自定义消息的 500 异常的解决方案(从此处扩展:https ://stackoverflow.com/a/10734690/654708 )

所有 Web API 控制器的基类

public abstract class ApiBaseController : ApiController
{
    protected internal virtual IHttpActionResult InternalServerError(Exception ex, string message = null)
    {
        var customMsg = String.IsNullOrWhiteSpace(message) ? "" : String.Format("Custom error message : {0}. ", message);
        var errorMsg = String.Format("{0}{1}", customMsg, ex);
        return new InternalServerErrorWithMessageResult(errorMsg);
    }
}

public class InternalServerErrorWithMessageResult : IHttpActionResult
{
    private readonly string message;

    public InternalServerErrorWithMessageResult(string message)
    {
        this.message = message;
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                       {
                           Content = new StringContent(message)
                       };
        return Task.FromResult(response);
    }
}

示例 Web API 控制器

public class ProductController : ApiBaseController
{
    public IHttpActionResult GetProduct(int id)
    {
        try
        {
            var product = productService.GetProduct(id);
            return Ok(product); // 200 w/ product
        } catch(Exception ex)
        {
           //return InternalServerError(ex); // Uses default InternalServerError
           return InternalServerError(ex, "My custom message here"); // Uses custom InternalServerError
        }
    }    

}
于 2014-12-27T21:57:42.873 回答