【问题标题】:OData controller returns different error for local and different for remote machineOData 控制器为本地机器返回不同的错误,为远程机器返回不同的错误
【发布时间】:2016-07-10 10:55:36
【问题描述】:

在我的项目中,我使用 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 以返回详细错误(在 web.config 中设置 &lt;httpErrors errorMode="Detailed" /&gt;),即使它可能是一个安全风险(在这种情况下不是真的,它是一个未发布到 Internet 的 Intranet 应用程序) ,但没有任何帮助。

感谢您的帮助。

【问题讨论】:

    标签: .net asp.net-web-api odata


    【解决方案1】:

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

    config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
    

    更多详情可以在这里找到:https://msdn.microsoft.com/en-us/library/system.web.http.httpconfiguration.includeerrordetailpolicy(v=vs.108).aspx

    【讨论】:

    • 谢谢,这就是我要找的,你是我的英雄。
    猜你喜欢
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多