OData控制器为本地返回不同的错误,而为远程计算机返回不同的错误 [英] OData controller returns different error for local and different for remote machine
问题描述
在我的项目中,我使用OData 3.0,并且需要处理来自SQL存储过程的错误.这是控制器代码:
In my project I use OData 3.0 and I need to handle error that comes from SQL stored procedure. Here is the controller code:
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引发异常.
In case of error in the stored procedure the exception is raised using SQL command RAISERROR.
在客户端,我收到两个不同的响应-如果从本地主机连接,则会收到以下消息:
On my client side I receive two different responses - if I connect from localhost, I get this:
{
"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)
...where I can read the error message (The following materials are not available on the line: 562942)
如果我在远程计算机上执行相同的操作,这是我得到的信息:
If I do the same thing from the remote computer, here is what I get back:
{
"odata.error":{
"code":"","message":{
"lang":"en-US","value":"An error has occurred."
}
}
}
我试图将IIS设置为返回详细错误(在web.config中设置<httpErrors errorMode="Detailed" />
),即使这可能是一种安全风险(在这种情况下并非如此,它是未发布到Internet的Intranet应用程序) ,但无济于事.
I tried to setup my IIS to return the detailed error (setting <httpErrors errorMode="Detailed" />
in web.config) even though it may be a security risc (not really in this case, it is an intranet application not published to the Internet), but nothing helped.
感谢您的帮助.
推荐答案
您需要像这样在HttpConfiguration
类上设置IncludeErrorDetailPolicy
属性:
You need to set the IncludeErrorDetailPolicy
property on the HttpConfiguration
class like this:
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
可在此处找到更多详细信息: https://msdn.microsoft.com/zh-cn/library/system.web.http.httpconfiguration.includeerrordetailpolicy(v = vs.108).aspx
More details can be found here: https://msdn.microsoft.com/en-us/library/system.web.http.httpconfiguration.includeerrordetailpolicy(v=vs.108).aspx
这篇关于OData控制器为本地返回不同的错误,而为远程计算机返回不同的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!