为 1 个网站的 2 个不同部分设置 2 个不同的自定义错误页面

Set up 2 different custom error pages for 2 different parts of 1 website

目前我的整个 ASP .NET 站点有 1 个自定义错误页面:

<system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="~/ErrorPage.aspx" />
</system.web>

但是,我想设置2个不同的错误页面,这样不同部分的错误会导致重定向到不同的错误页面。例如:

有可能吗?

不能,显示的页面只能根据HTTP结果码,如500、404等可读on this page。从那里引用:

"A better approach is to use a custom error page, which entails creating and designing the custom error page and specifying its URL in the section's defaultRedirect attribute. You can even have multiple custom error pages for different HTTP error statuses."

您可以在 try/catch 块中使用自己的错误处理并在那里手动进行重定向。

ASP.NET 允许在子目录中覆盖 web.config 中的某些设置;这包括错误处理。只需创建:

<system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="~/ErrorPage1.aspx" />
</system.web>

作为Part1\web.config,并且

<system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="~/ErrorPage2.aspx" />
</system.web>

作为Part2\web.config.