在ASP.NET中指定出错页面,不让代码外泄!

在ASP.NET中指定出错页面,不让代码外泄!在ASP.NET中原始的出错页面会暴露部分源代码,由此带来了潜在的安全隐患。
ASP.NET允许应用程序出错时显示用户指定的页面,方法是在web.config文件中修改配置信息。
<? xml version="1.0" encoding="UTF-8" ?> 

<configuration>

  <system.web>

    <customErrors mode="On" defaultRedirect="ErrorPage.htm" />

  </system.web>

</configuration>

上面的配置信息明确指定了当应用程序出错的时候显示用户定制的页面ErrorPage.htm。并且因为各个应用程序有自己独立的web.config配置文件,所以应用程序之间不会互相干扰。

其中,mode属性表示是否启用定制的用户页面,它可取三个值,如下所示:
On 启用定制的出错页面
Off 禁用定制的出错页面
RemoteOnly 启用定制的出错页面但仅展示给远程用户

defaultRedirect属性表示用户定制出错页面的文件名。

ErrorPage.htm中,一般的内容是这样的:

英文:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Internal Error</title>
</head>
<body>
    <h1>
        We're sorry, an internal error occurred that prevents the request to complete.</h1>
    <p>
        Our supporting staff has been notified with this error and will address this issue
        shortly. We profusely apologize for the <b>inconvenience</b> and for any damage
        this may cause. You might want to try the same action at later time.
    </p>
</body>
</html>


中文:

<h1>
    我们很抱歉。内部错误无法完成您的要求。</h1>
<p>
    我们的支援人员已通知有此错误,将解决这一问题。我们对带来的不便深表歉意。你可以过一段时间再试一下。
</p>



 

原文地址:https://www.cnblogs.com/WestGarden/p/3138384.html