【问题标题】:IHttpActionResult - How to use?IHttpActionResult - 如何使用?
【发布时间】:2013-11-24 23:35:06
【问题描述】:

我已经看到使用一些新的 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);
}

【问题讨论】:

  • 我发现 response = InternalServerError(ex);解决 500 问题。我仍在寻找接受字符串消息的 Unauthorized 示例。

标签: asp.net asp.net-web-api2


【解决方案1】:

根据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 版本或发出拉取请求以尝试将其放入主分支)。


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

【讨论】:

    【解决方案2】:

    您可以在此处阅读有关 Unathorized() 的更多信息 How do you return status 401 from WebAPI to AngularJS and also include a custom message? 如果找到另一种方式 - 不抛出异常并使用 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

    【讨论】:

      【解决方案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
              }
          }    
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-19
        • 2014-02-21
        • 1970-01-01
        相关资源
        最近更新 更多