OData 控制器 returns 本地错误不同,远程机器错误不同

OData controller returns different error for local and different for remote machine

在我的项目中,我使用 OData 3.0,我需要处理来自 SQL 存储过程的错误。这是控制器代码:

public class StartProductionBatchListController : ODataController {
    private SitContext<StartProductionBatchModel> db = new SitContext<StartProductionBatchModel>();

    [EnableQuery]
    [SITAuthorize("/production/ordersOverview")]
    public DbRawSqlQuery<StartProductionBatchModel> GetStartProductionBatchList([FromODataUri] string entryId)
    {
        Dictionary<string, string> parameters = new Dictionary<string, string>();

        parameters.Add("ENTRY_ID", entryId);

        return db.ExecuteProcedure("StartProductionBatch", parameters);
    }
}

如果存储过程出错,使用 SQL 命令 RAISERROR 会引发异常。

在我的客户端,我收到了两个不同的响应 - 如果我从本地主机连接,我得到这个:

{
  "odata.error":{
    "code":"","message":{
      "lang":"en-US","value":"An error has occurred."
    },"innererror":{
      "message":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","type":"System.InvalidOperationException","stacktrace":"","internalexception":{

        "message":"The following materials are not available on the line: 562942",

"type":"System.Data.SqlClient.SqlException","stacktrace":"

    ... there is the entire stack here

      }
    }
  }
}

...哪里可以看到报错信息(以下资料不在线:562942)

如果我从远程计算机上做同样的事情,这就是我得到的结果:

{
  "odata.error":{
    "code":"","message":{
      "lang":"en-US","value":"An error has occurred."
    }
  }
}

我试图将我的 IIS 设置为 return 详细错误(在 web.config 中设置 <httpErrors errorMode="Detailed" />),即使它可能是一个安全风险(在这种情况下不是真的,它是未发布到 Internet 的 Intranet 应用程序),但没有任何帮助。

感谢您的帮助。

您需要像这样在 HttpConfiguration class 上设置 IncludeErrorDetailPolicy 属性:

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

可以在此处找到更多详细信息:https://msdn.microsoft.com/en-us/library/system.web.http.httpconfiguration.includeerrordetailpolicy(v=vs.108).aspx