使用 web.config 阻止 IIS 7.5 显示默认错误页面

发布于 2024-11-19 14:09:29 字数 1764 浏览 1 评论 0原文

我正在尝试将自定义错误页面添加到我的网络应用程序中。到目前为止,我已将其添加到元素下的 web.config 文件中:

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

这对于 .NET 涉及的错误(例如包含 .aspx 扩展名的 url)非常有效。不过,我还希望为诸如 www.example.com/dasda 之类的 url 显示自定义错误。

目前,当我请求诸如上面的页面时,IIS 7.5 会显示它自己的错误消息。我已在元素下添加了此内容:

<httpErrors >
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" path="~/404.aspx" responseMode="ExecuteURL"  />
    <remove statusCode="500" subStatusCode="-1" />
    <error statusCode="500" path="~/500.aspx" responseMode="ExecuteURL" />
</httpErrors>

我认为这将使 IIS 显示自定义错误页面而不是默认页面,但情况似乎并非如此。

我知道我可以在 IIS 本身中设置自定义错误页面,但对于我的情况来说,理想的解决方案是在 web.config 中进行配置。

我已尝试按照建议将其添加到 Page_Load 事件上的自定义错误页面中 此处

            Response.TrySkipIisCustomErrors = true;

但是,它并没有阻止默认 IIS 页面显示,而不是我的自定义错误页面。我也尝试了此处的建议:

<httpErrors >
    <remove statusCode="404" subStatusCode='-1' />
    <error statusCode="404" path="~/404.aspx" prefixLanguageFilePath='' responseMode="Redirect"  />
    <remove statusCode="500" subStatusCode='-1' />
    <error statusCode="500" path="~/500.aspx" prefixLanguageFilePath='' responseMode="Redirect" />
  </httpErrors>

但这也没有工作了。

那么有没有办法通过在web.config文件中配置设置来阻止IIS显示默认错误页面呢?

I am trying to add custom error pages to my web application. So far I have added this to my web.config file under the element:

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

This works fine for errors that .NET touches for example a url that contains the .aspx extension. However I also want custom errors to display for a url such as www.example.com/dasda

Currently when I request a page such as the above IIS 7.5 displays it's own error message. I have added this under the element:

<httpErrors >
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" path="~/404.aspx" responseMode="ExecuteURL"  />
    <remove statusCode="500" subStatusCode="-1" />
    <error statusCode="500" path="~/500.aspx" responseMode="ExecuteURL" />
</httpErrors>

I thought that this would make IIS display a custom error page instead of it's default ones but this doesn't seem to be the case.

I am aware that I can set a custom error page in IIS itself but an ideal solution for my situation would be to have this configurable in the web.config.

I have tried adding this into my custom error pages on the Page_Load event as suggested here :

            Response.TrySkipIisCustomErrors = true;

However it did not stop the default IIS page from showing in place of my custom error page. I have also tried what is suggested here:

<httpErrors >
    <remove statusCode="404" subStatusCode='-1' />
    <error statusCode="404" path="~/404.aspx" prefixLanguageFilePath='' responseMode="Redirect"  />
    <remove statusCode="500" subStatusCode='-1' />
    <error statusCode="500" path="~/500.aspx" prefixLanguageFilePath='' responseMode="Redirect" />
  </httpErrors>

But this has also not worked.

So is there a way to prevent IIS from displaying default error pages by configuring settings in the web.config file?

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

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

发布评论

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

评论(3

忆梦 2024-11-26 14:09:29

我遇到的问题是默认情况下有一个属性 errorMode 具有以下选项:DetailedLocalOnly、Custom 或Detailed。

如果 errorMode 属性像我一样未指定,那么它默认为DetailedLocalOnly (ref)。这意味着我不会看到显示的自定义错误。

有效的配置设置是:

<httpErrors errorMode="Custom">
    <remove statusCode="404" subStatusCode='-1' />
    <remove statusCode="500" subStatusCode='-1' />
    <error statusCode="404" path="/404.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL"  />
    <error statusCode="500" path="/500.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL" />
  </httpErrors>

errorMode="Custom" 可以方便地测试自定义页面是否正常工作,但如果省略或明确设置为 errorMode="DetailedLocalOnly" 以进行调试,可能会更方便。

The problem I was encountering was that by default has an attribute errorMode with the following options: DetailedLocalOnly, Custom or Detailed.

If the errorMode attribute is left unspecified as I was doing then it defaults to DetailedLocalOnly (ref). Which means I would not have seen the custom error that was displayed.

The configuration settings that worked were:

<httpErrors errorMode="Custom">
    <remove statusCode="404" subStatusCode='-1' />
    <remove statusCode="500" subStatusCode='-1' />
    <error statusCode="404" path="/404.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL"  />
    <error statusCode="500" path="/500.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL" />
  </httpErrors>

The errorMode="Custom" is handy to test the custom pages are working correctly but is probably more handy when left omitted or set explicitly to errorMode="DetailedLocalOnly" for debugging purposes.

遗弃M 2024-11-26 14:09:29

我认为您需要强制 IIS 对用户请求的任何资源使用 CustomErrorModule。
尝试将以下内容添加到您的网络配置中

 <modules>
      <add name="CustomErrorModule" type="customErrorModule" preCondition="" />
 </modules>

另请查看以下链接

https:// serverfault.com/questions/53712/in-iis-7-how-do-i-set-up-a-default-error-document-for-any-error

I think you need to force IIS to use the CustomErrorModule for any resource requested by the user.
Try to add the below to your web config

 <modules>
      <add name="CustomErrorModule" type="customErrorModule" preCondition="" />
 </modules>

Have also a look at the below link

https://serverfault.com/questions/53712/in-iis-7-how-do-i-set-up-a-default-error-document-for-any-error

谁把谁当真 2024-11-26 14:09:29

如果您有来自服务器的 JSON 响应,则通过设置 existingResponse="PassThrough",您可以指示 IIS 传递 HTTP 错误响应而不对其进行处理。这允许服务器的 JSON 响应直接发送到客户端。

</system.webServer>   
    <!-- Avoid showing the default IIS error pages and display JSON response -->
    <httpErrors existingResponse="PassThrough" />
</system.webServer> 

In the case you have a JSON response coming from your server, by setting existingResponse="PassThrough", you instruct IIS to pass through the HTTP error response without handling it. This allows your server's JSON response to be sent to the client directly.

</system.webServer>   
    <!-- Avoid showing the default IIS error pages and display JSON response -->
    <httpErrors existingResponse="PassThrough" />
</system.webServer> 

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