ASP.Net Core 500错误An error occurred while processing your request

今天在调用ASP.Net Core WebAPI接口的时候报500错误,具体内容如下:

An error occurred while processing your request.
Development Mode
Swapping to Development environment will display more detailed information
about the error that occurred.
Development environment should not be enabled in deployed applications,
as it can result in sensitive information from exceptions being displayed to end users.
For local debugging, development environment can be enabled by
setting the ASPNETCORE_ENVIRONMENT environment variable to Development,
and restarting the application.

言下之意就是,程序发生异常,但因为不是开发环境,无法展示详细信息。

如果要查看错误信息,可以修改Web.config文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<configuration>
<!--
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
-->

<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\Application.exe" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>

将上面的value="Production"改为value="Development",重启一下网站后即可。但由于我的程序依赖了Node.js,重启后报错,要求安装Node.js。等我安装完Node.js后,重启网站还是说缺少Node.js,估计是整个IIS,或者http服务需要重启,这个代价太高了,不能这么做,其它的部署在线上的网站都会受影响的。

最后,几经排查,原来是配置文件中缺少了一个配置项引起的。但是我差点被一开始的提示给误导了,一直以为程序处于开发者模式,怎么就不能看详细信息呢,时间都浪费在了上面。其实是处于生产模式的,提示只是告诉你,如果要看具体信息,需要调整为开发者模式,并且告诫你开发者模式不适合放在生产环境下使用。本质还是程序有问题导致的。

avatar

chilihotpot

You Are The JavaScript In My HTML