简体   繁体   English

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

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

Currently I'm having 1 custom error page for my whole ASP .NET site: 目前,我的整个ASP .NET网站都有1个自定义错误页面:

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

However, I would like to set up 2 different error pages, so that error on different part of the side will cause redirection to different error pages. 但是,我想设置2个不同的错误页面,以便侧面的不同部分的错误将导致重定向到不同的错误页面。 For example: 例如:

  • When users encounter an error in a page within http://example.com/Part1/ they will be redirect to ErrorPage1.aspx . 当用户在http://example.com/Part1/内的页面中遇到错误时,他们将被重定向到ErrorPage1.aspx
  • When users encounter an error in a page within http://example.com/Part2/ they will be redirect to ErrorPage2.aspx 当用户在http://example.com/Part2/中的页面中遇到错误时,他们将被重定向到ErrorPage2.aspx

Is it possible at all? 有可能吗?

ASP.NET allows certain settings within web.config to be overridden in subdirectories; ASP.NET允许在子目录中覆盖web.config某些设置; this includes the error handling. 这包括错误处理。 Simply create: 只需创建:

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

as Part1\\web.config , and 作为Part1\\web.config ,和

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

as Part2\\web.config . 作为Part2\\web.config

No, the page shown can only be based on the HTTP result code, like 500, 404, etc. as can be read on this page . 不,显示的页面只能基于HTTP结果代码,例如500、404等,可以在此页面上阅读。 Quoted from there: 从那里引用:

"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." “更好的方法是使用自定义错误页面,这需要创建和设计自定义错误页面,并在该部分的defaultRedirect属性中指定其URL。对于不同的HTTP错误状态,您甚至可以拥有多个自定义错误页面。”

You could use your own errorhandling in try/catch blocks and do the redirects manually there. 您可以在try / catch块中使用自己的错误处理,并在那里手动进行重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM