当我对 API 进行 $http 调用时,它从我的 MVC Controller 抛出异常,我使用
记录返回到我的 Angular 代码的异常消息.then(function success(response){ // success },
function error(response){ console.log(JSON.stringify(response);};})
这有效并显示自定义异常消息。但是,当我在 IIS 上的远程服务器上部署应用程序时,抛出异常时,response
参数始终为:
{"data":{"Message":"An error has occurred."},"status":500,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":...,"headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"Internal Server Error"}
...基本上,它不包含自定义异常消息,它总是简单地显示“发生错误”。
这可能是一个路由错误,也就是异常实际上没有到达我的 Angular 端吗?我的逻辑是因为远程 URL 如下所示:
website.com/extension
,在我的本地是 website.com
最佳答案
默认情况下,出于安全原因,从远程计算机访问时不会将完整的错误消息发送到客户端,因为它可能会泄露敏感数据。您可以通过将 customErrors
模式设置为 Off
来修改 web.config
以将详细信息返回到远程计算机。但是,您不应在未首先考虑安全影响的情况下这样做。
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
更好的做法是向客户端提供通用错误消息“抱歉,发生了意外错误!”并将服务器上的实际错误消息记录在日志中。
关于c# - 当应用程序位于远程计算机上时,异常未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36066831/