【问题标题】:What is the difference between customErrors and httpErrors?customErrors 和 httpErrors 有什么区别?
【发布时间】:2011-01-29 14:47:18
【问题描述】:

ASP.NET MVC 应用程序中 web.config 文件的 customErrorshttpErrors 部分有什么区别?

每个部分的使用指南是什么?

【问题讨论】:

  • 恕我直言 - HttpError 是 IIS 级别的错误消息处理,而 CustomError 是 ASP.Net 处理 Web 应用程序中的错误。但很想知道更多...

标签: web-config iis-7.5


【解决方案1】:

免责声明:这是根据我的经验,未经证实的事实。

两者都用于定义网站的错误处理,但不同的软件引用不同的配置元素。

customErrors 是旧版(向后兼容)元素,由 Visual Studio Development Server(又名 VSDS 或 Cassini)使用。

httpErrors 是新元素,仅供 IIS7 使用。

这突出了在使用 VSDS 而不是本地 IIS 开发 ASP.NET 网站时可能的问题。

另外,refer to this post by myself 关于如何使用 IIS7 处理错误消息,如果您希望完全控制错误输出。

总结:

  • VSDS 中开发 - 使用customErrors
  • 将网站发布到IIS6 - 使用customErrors
  • 将网站发布到IIS7 - 使用httpErrors

如果您使用VSDS 开发但发布到IIS7,那么我想您将需要两者。

【讨论】:

  • customErrors 用于 asp.net。 httpErrors 用于 IIS7,因此可以处理不通过 .net 处理程序的内容(例如 .png、.js 等)。如果您想要非 .net 内容类型的错误页面,请使用 IIS 错误页面(IIS7 的 httpErrors ,IIS6 的用户界面。)
  • 我建议安装和使用带有 Visual Studio 的 IIS 7 Express 以进行调试。与 Cassini 不同,它将使用与常规 IIS 7 相同的配置选项。
  • 不再需要使用 @johnB 的 customErrors 。 太有必要了吗? 1.) 在“ASP”部分下启用“向浏览器发送错误”。调试属性 2.) 在“错误页面/编辑功能设置”下,选择“详细错误”。 3.) 在 IE 中禁用“显示友好的 HTTP 错误消息” stackoverflow.com/questions/2640526/…
【解决方案2】:

Web config 中的Errors 部分用于提供自定义http 错误处理方法,有两个部分,一个位于system.web 部分中的customErrors 和另一个位于system.webServer 部分中的httpErrors(如下所示)

自定义错误: 此部分在 IIS 7 引入之前使用,IIS 6 5 以及在完全使用此部分根据 http 状态代码处理自定义 http 错误之前。

httpErrors: 如果请求的页面扩展名注册到 ISAPI dll(.aspx、ashx、.asmx、.svc 等),则 IIS 7 及更高版本使用此部分以及 customErrors 部分来处理基于其文件扩展名的自定义 http 错误像 index.aspx 然后 IIS 从 customeErrors 部分获取设置,否则它从 httpErrors 获取设置(IIS 7 托管模式必须设置为集成心情而不是经典)

以下是 404 错误处理检查链接的示例:

httperrors vs customerrors in webconfig , iis, asp.net

【讨论】:

    【解决方案3】:

    *2016 年 4 月更新

    .net 代码抛出异常(404、403、500 等)时使用 customErrors 属性,而 IIS 本身抛出异常时使用 httpErrors 属性。

    • /myfakeextensionslessurl --> httpErrors 404
    • /myfakeaspsx.aspx --> customErrors 404
    • /myfakeimage.jpg --> httpErrors 404
    • /throw500.apx --> customErrors 500
    • /throw500 --> customErrors 500

    试图正确配置它有很多陷阱。因此,如果您正在寻找一个简单的示例,最好的 2 个选项是:

    示例 1:使用 html 页面

    <system.web>
      <customErrors mode="RemoteOnly" defaultRedirect="/Error500.html" redirectMode="ResponseRewrite">
        <error statusCode="403" redirect="/Error403.html" />
        <error statusCode="404" redirect="/Error404.html" />
        <error statusCode="500" redirect="/Error500.html" />
      </customErrors>
    </system.web>
    <system.webServer>
      <httpErrors errorMode="DetailedLocalOnly" existingResponse="Auto">
        <remove statusCode="403" />
        <remove statusCode="404" />
        <remove statusCode="500" />
        <error statusCode="403" responseMode="File" path="Error403.html" />
        <error statusCode="404" responseMode="File" path="Error404.html" />
        <error statusCode="500" responseMode="File" path="Error500.html" />
      </httpErrors>
    </system.webServer>
    

    示例 2:使用 aspx 页面

    <system.web>
      <customErrors mode="RemoteOnly" defaultRedirect="/Error500.html" redirectMode="ResponseRewrite">
        <error statusCode="403" redirect="/Error403.aspx" />
        <error statusCode="404" redirect="/Error404.aspx" />
        <error statusCode="500" redirect="/Error500.aspx" />
      </customErrors>
    </system.web>
    <system.webServer>
      <httpErrors errorMode="DetailedLocalOnly" existingResponse="Auto">
        <remove statusCode="403" />
        <remove statusCode="404" />
        <remove statusCode="500" />
        <error statusCode="403" responseMode="ExecuteURL" path="Error403.aspx" />
        <error statusCode="404" responseMode="ExecuteURL" path="Error404.aspx" />
        <error statusCode="500" responseMode="ExecuteURL" path="Error500.aspx" />
      </httpErrors>
    </system.webServer>
    

    在 aspx 错误页面中你需要做这样的事情(例如 404 页面):

    <% 
        Response.StatusCode = 404;
        Response.TrySkipIisCustomErrors = true;
     %>
    

    注意:在 customErrors 部分中使用无扩展名的 URL 是不可能的!(没有 hack)

    一种解决方法是禁用自定义错误并让 http 错误处理自定义页面。有朋友创建了这样的设置,有时间我会分享代码。

    背景

    一个好的自定义错误页面将:

    1. 在本地访问问题页面时显示真正的异常
    2. 远程访问问题页面时显示自定义页面
    3. 不会重定向,只是显示错误页面内容(因为seo的原因)
    4. 将显示正确的状态代码

    所以要澄清我们配置中的一些选项:

    1. &lt;customErrors mode="RemoteOnly"。您可以在此处指定:OnOffRemoteOnly

      • On = 始终显示自定义错误页面
      • Off = 始终显示真正的错误
      • RemoteOnly = 在本地显示错误,但在远程显示自定义错误页面。 所以我们想要 RemoteOnly 声明 1
    2. &lt;customErrors redirectMode="ResponseRewrite"。您可以在此处指定:ResponseRedirectResponseRewriteResponseRedirect 模式会将错误页面重定向到自定义错误页面。对于链接爬虫 (SEO),这将导致 302 -> 500,但您希望链接爬虫得到 500 错误。

    3. &lt;httpErrors errorMode="DetailedLocalOnly"。这相当于customErrors 模式。您拥有的选项:CustomDetailedDetailedLocalOnly

    一篇对我帮助很大的好博文是:http://benfoster.io/blog/aspnet-mvc-custom-error-pages

    【讨论】:

    • 什么关系 customErrors - httpErrors 和 IIS 配置,如 ASP 中的 “向浏览器发送错误” - 调试属性“错误页面/编辑功能设置”, “详细错误”。 ? stackoverflow.com/questions/2640526/…
    【解决方案4】:

    &lt;customErrors&gt;&lt;httpErrors&gt;


    &lt;customErrors&gt;

    • 在 IIS7+ 中仍然可用
    • 为 ASP.NET 处理的请求指定自定义错误页面
    • 仅处理 ASP.NET 应用程序中的请求
    • 不处理 HTML 文件或目录(“友好”)URL 等静态文件

    &lt;httpErrors&gt;

    • 在 IIS7 中引入
    • 为 IIS 处理的请求指定自定义错误页面
    • 处理 ASP.NET 应用程序内的请求 AND/OR 处理 - ASP.NET 应用程序外的请求 *
    • 处理所有文件和 URL *

    注意:不再需要使用customErrors

    引用来源:Custom 404 and error pages in ASP.NET(优秀文章)


    ExecuteURL 提供动态内容,例如 .aspx 页面(path 值必须是服务器相对 URL):

    <system.webServer>
      <httpErrors errorMode="Custom" existingResponse="Auto" defaultResponseMode="ExecuteURL" >
        <remove statusCode="404"/>
        <error statusCode="404" responseMode="ExecuteURL" path="/error.aspx" />
      </httpErrors>
    </system.webServer>
    

    File 提供自定义错误文件,例如 .html 页面:

    <system.webServer>
      <httpErrors errorMode="Custom" existingResponse="Auto" defaultResponseMode="File" >
        <remove statusCode="404"/>
        <error statusCode="404" path="404.html" />
      </httpErrors>
    </system.webServer>
    

    参考:HTTP Errors (www.iis.net)

    有关更多详细信息,请阅读上面的 www.iis.net 链接

    【讨论】:

    • 可能有用** stackoverflow.com/questions/2640526/…** 如果将 httpErrors“向浏览器发送错误” 和 错误页面
    • +1 注释 it's no loger necesary to use customErrors 和引用,这真的是我所追求的信息 :-)
    猜你喜欢
    • 2018-04-30
    • 2012-01-09
    • 2010-10-02
    • 2011-12-12
    • 2010-09-16
    • 2012-03-14
    • 2012-02-06
    • 2011-02-25
    • 2011-11-22
    相关资源
    最近更新 更多