web.config发起的服务器错误的customError

时间:2012-11-20 22:29:27

标签: asp.net web-config

注意

我已经确定我在下面描述的问题特定于加载web.config文件中指定的加载DLL文件的错误。即使在web.config错误的情况下,我也想提出一个用户友好的错误。

结束注释

当我的ASP.Net应用程序遇到服务器错误时,我希望它向用户显示自定义错误消息,而不是以下默认的可怕消息。

Server Error in '/' Application.

Runtime Error

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine. 

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".


<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>

Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.


<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
    </system.web>
</configuration>

我写了一个非常简单的HTML页面并将其放在我的应用程序的根目录中。它叫做MaintenancePage.htm。

我已将web.config文件设置为以下内容:

<customErrors mode="RemoteOnly" defaultRedirect="MaintenancePage.htm">
  <error statusCode="404" redirect="PageNotFound.aspx" />
</customErrors>

我还尝试了~/MaintenancePage.htmhttp://[mysite]/MaintenancePage.htm。这些选项似乎都不起作用。

我测试它的方法是重命名我的项目所依赖的DLL,然后在Web浏览器中加载该站点。我希望由于出现错误并设置了defaultRedirect,因此显示错误页面应该没有问题,但是,我显然是错的。

我正在搜索有关此问题的内容,似乎大多数人都试图重定向到aspx页面,并且在执行此操作时遇到错误。许多人甚至报告他们无法将aspx页面加载为defaultRedirect,但他们可以加载html页面。

我在这里做错了什么?

我应该注意,我是在公司防火墙之外的其他网络上进行测试,因此根据文档将RemoteOnly更改为On不是问题。在测试中,将RemoteOnly更改为On与预期无关。

1 个答案:

答案 0 :(得分:2)

RemoteOnly 更改为开启

仅限远程:

  

指定仅向远程客户端显示自定义错误    ASP.NET错误显示给本地主机

此外,您的网址可能是绝对的或相对的。

Read more about the customErrors settings

要观看它在本地工作:

<customErrors mode="On" defaultRedirect="/MaintenancePage.htm">
  <error statusCode="404" redirect="/PageNotFound.aspx" />
</customErrors>

更容易测试。

  1. 尝试错误的网址并观看您的404错误接管
  2. 将此信息放在default.aspx页&lt;%throw new Exception(“gaah”); %GT;
  3. 还有另外一件事要考虑:错误记录。可以通过ELMAH轻松实现。