Mvc4 Web Api空响应时不是200

时间:2012-05-07 19:31:07

标签: asp.net-mvc-4 asp.net-web-api

当调用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);
  }

编辑:当我在本地点击服务时,包含正文。似乎问题是从远程计算机上访问服务时。

1 个答案:

答案 0 :(得分:2)

试试这个:

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = 
            IncludeErrorDetailPolicy.Always