1

当调用 Action 并引发特定异常时,我使用 ExceptionFilterAttribute 将错误转换为不同的响应,如 HttpStatusCode.BadRequest。这一直在本地工作,但我们将它推送到服务器,现在当我收到 BadRequest 时,我没有在响应中获得任何信息。我错过了什么?

    public override void OnException(HttpActionExecutedContext actionExecutedContext)
    {
        MyException ex = actionExecutedContext.Exception as MyException;
        if (ex == null)
        {
            base.OnException(actionExecutedContext);
            return;
        }

        IEnumerable<InfoItem> items = ex.Items.Select(i => new InfoItem
                                                                       {
                                                                           Property = i.PropertyName,
                                                                       Message = i.ToString()
                                                                   });

    actionExecutedContext.Result = new HttpResponseMessage<IEnumerable<InfoItem>>(items, HttpStatusCode.BadRequest);
  }

编辑:当我在本地点击服务时,正文包括在内。似乎问题出在从远程机器上访问服务时。

4

1 回答 1

2

尝试这个:

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = 
            IncludeErrorDetailPolicy.Always
于 2012-05-14T11:53:41.347 回答