在 ASP.Net 网站上实现自定义错误页面

发布于 2024-08-19 10:17:03 字数 364 浏览 2 评论 0原文

我有一个 ASP.Net 网站,我想使用自定义错误页面。我将以下代码放入 web.config 中

<customErrors mode="On" defaultRedirect="~/error.aspx">
    <error statusCode="404" redirect="~/error.aspx" />
</customErrors>

问题是当我转到不存在的 URL 时仍然使用 IIS 管理器中指定的 404 错误页面。

问题:如何让它使用我创建的 error.aspx 页面?为什么 IIS 管理器中的设置会覆盖 web.config?

I have an ASP.Net website and I want to use a custom error page. I put the following code in my web.config

<customErrors mode="On" defaultRedirect="~/error.aspx">
    <error statusCode="404" redirect="~/error.aspx" />
</customErrors>

The problem is when i go to a URL that does not exist is still uses the 404 error page specified in IIS Manager.

Question: How can I make it use the error.aspx page I have created? Why do the settings in IIS Manager override the web.config?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小清晰的声音 2024-08-26 10:17:03

尝试这种方式,几乎相同..但这就是我所做的,并且正在工作。

<configuration>
    <system.web>
       <customErrors mode="On" defaultRedirect="apperror.aspx">
          <error statusCode="404" redirect="404.aspx" />
          <error statusCode="500" redirect="500.aspx" />
       </customErrors>
    </system.web>
</configuration> 

或者如果紧急需要,尝试从 IIS 设置更改 404 错误页面。

Try this way, almost same.. but that's what I did, and working.

<configuration>
    <system.web>
       <customErrors mode="On" defaultRedirect="apperror.aspx">
          <error statusCode="404" redirect="404.aspx" />
          <error statusCode="500" redirect="500.aspx" />
       </customErrors>
    </system.web>
</configuration> 

or try to change the 404 error page from IIS settings, if required urgently.

岁月无声 2024-08-26 10:17:03

有两种方法可以为 ASP.NET 站点配置自定义错误页面:

  1. Internet 信息服务 (IIS) 管理器(GUI)
  2. web.config 文件

本文介绍了如何执行每种操作:

您的 error.aspx 页面未显示的原因可能是您的 web.config 中有错误。请尝试以下操作:

<configuration>
   <system.web>
      <customErrors defaultRedirect="error.aspx" mode="RemoteOnly">
         <error statusCode="404" redirect="error.aspx"/>
      </customErrors>
   </system.web>
</configuration>

您可能需要确保IIS 管理器 - 功能委派中的错误页面设置为读/写

IIS 管理器:功能委派面板

此外,此答案可能会帮助您配置 web.config 文件:

There are 2 ways to configure custom error pages for ASP.NET sites:

  1. Internet Information Services (IIS) Manager (the GUI)
  2. web.config file

This article explains how to do each:

The reason your error.aspx page is not displaying might be because you have an error in your web.config. Try this instead:

<configuration>
   <system.web>
      <customErrors defaultRedirect="error.aspx" mode="RemoteOnly">
         <error statusCode="404" redirect="error.aspx"/>
      </customErrors>
   </system.web>
</configuration>

You might need to make sure that Error Pages in IIS Manager - Feature Delegation is set to Read/Write:

IIS Manager: Feature Delegation panel

Also, this answer may help you configure the web.config file:

红焚 2024-08-26 10:17:03
<customErrors defaultRedirect="~/404.aspx" mode="On">
    <error statusCode="404" redirect="~/404.aspx"/>
</customErrors>

如果文件扩展名已知(.html、.aspx 等),上面的代码仅适用于“找不到页面错误-404”

此外,您还可以将扩展名未知或不正确的客户错误设置为

.aspwx.vivaldo。您必须在 web.config 中添加 httperrors 设置,

<httpErrors  errorMode="Custom"> 
       <error statusCode="404" prefixLanguageFilePath="" path="/404.aspx"         responseMode="Redirect" />
</httpErrors>
<modules runAllManagedModulesForAllRequests="true"/>

它必须位于

<customErrors defaultRedirect="~/404.aspx" mode="On">
    <error statusCode="404" redirect="~/404.aspx"/>
</customErrors>

Code above is only for "Page Not Found Error-404" if file extension is known(.html,.aspx etc)

Beside it you also have set Customer Errors for extension not known or not correct as

.aspwx or .vivaldo. You have to add httperrors settings in web.config

<httpErrors  errorMode="Custom"> 
       <error statusCode="404" prefixLanguageFilePath="" path="/404.aspx"         responseMode="Redirect" />
</httpErrors>
<modules runAllManagedModulesForAllRequests="true"/>

it must be inside the <system.webServer> </system.webServer>

树深时见影 2024-08-26 10:17:03
<system.webServer>     
<httpErrors errorMode="DetailedLocalOnly">
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="your page" responseMode="Redirect" />
    </httpErrors>
</system.webServer>
<system.webServer>     
<httpErrors errorMode="DetailedLocalOnly">
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="your page" responseMode="Redirect" />
    </httpErrors>
</system.webServer>
梦太阳 2024-08-26 10:17:03

您的结束标记中是否存在拼写错误,即:

</CustomErrors> instead of </CustomError>?

Is it a spelling error in your closing tag ie:

</CustomErrors> instead of </CustomError>?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文