zoukankan      html  css  js  c++  java
  • [ASP.net]web.config的customErrors与httpErrors的区别

    之前一直知道设置web.config(其实就是xml文件)的customErrors的error来指向自定义的错误页的URL,但是今天在调试的时候发现customErrors无法跳转到自定义的页面,在网上找了半天才了解还有httpErrors可以设置跳转自定义错误页。

    web.config文件如下

    <?xml version="1.0"?>
    <configuration>
      <system.web>
        <compilation debug="true" targetFramework="4.5"/>
        <httpRuntime targetFramework="4.5"/>
        <customErrors defaultRedirect="ErrorPagesCustomErr.html" mode="On">
          <error statusCode="403" redirect="ErrorPagesCustomErr403.html"/>
          <error statusCode="404" redirect="ErrorPagesCustomErr404.html"/>
        </customErrors>
      </system.web>
      <system.webServer>
        <httpErrors errorMode="Custom" defaultResponseMode="File">
          <error statusCode="404" subStatusCode="0" path="ErrorPagesHttpErr404.html"/>
        </httpErrors>        
      </system.webServer>
    </configuration>

    简单来说,存取静态档案(如.js、.html、.css、.jpg…)发生错误会依照httpErrors设定办事;

    由.NET处理程序接手的URL(例如:.aspx、.ashx、.svc、MVC注册路由),出错时则看customErrors裡的设定。

    http://localhost:2413/Default.aspx    --->正确的url

    http://localhost:2413/Default1111.aspx     --->由customErrors控制

    http://localhost:2413/Default.aspx1111      --->由httpErrors控制

    详细的解释可以访问下面链接

    http://blog.csdn.net/a351945755/article/details/21003249

    http://blog.darkthread.net/post-2015-11-10-customerrors-and-httperrors.aspx

     原文如下

    被抽考IIS網站的自訂HTTP 404錯誤網頁設定,學到新東西也釐清一些觀念,筆記備忘。

    以Windows 2008 R2 IIS 7.5為例,網站管理介面有兩處可以自訂錯誤頁面,上方的ASP.NET區的.NET Error Pages與下方IIS區的Error Pages:

    兩個設定介面有點不同,試著各自加上HTTP 404設定,但導向不同網頁,.NET Error Pages設定指向/NotFound/SystemWeb404.html:

    Error Pages指向/NotFound/SystemWebServer404.html

    設定結果會反應在web.config,.NET Error Pages設定被寫入system.web/customErrors,Error Pages則是寫到system.webServer/httpErrors

    排版顯示純文字
     
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.web>
            <customErrors mode="On">
        <error statusCode="404" redirect="/NotFound/SystemWeb404.html"/>
            </customErrors>
        </system.web>
        <system.webServer>
            <urlCompression doDynamicCompression="true" />
            <httpErrors>
                <remove statusCode="404" subStatusCode="-1" />
                <error statusCode="404" prefixLanguageFilePath="" 
                  path="/NotFound/SystemWebServer404.html" responseMode="ExecuteURL" />
            </httpErrors>
        </system.webServer>
    </configuration>

    這兩個設定有什麼不同呢?簡單來說,存取靜態檔案(如.js、.html、.css、.jpg…)發生錯誤會依照httpErrors設定辦事;由.NET處理程序接手的URL(例如:.aspx、.ashx、.svc、MVC註冊路由),出錯時則看customErrors裡的設定。

    以下是簡單示範,輸入不存在的blah.gif看到的是SystemWebServer404.html、輸入不存在的blah.aspx則是SystemWeb.404.html,故得證。

    補充一點:httpErrors有個errorMode屬性,預設為DetailedLocalOnly,相當於customErrors mode="RemoteOnly",故在本機測試將看不到自訂錯誤頁,要改成Custom才看得到。這是IIS 7起加入的行為,還停在IIS 6的腦袋沒意識到有差異,花了點時間才搞定,特別加記一筆。

  • 相关阅读:
    如何只通过Sandboxed Solution启动一个定时执行的操作
    创建与SharePoint 2010风格一致的下拉菜单 (续) 整合Feature Custom Action框架
    创建与SharePoint 2010风格一致的下拉菜单
    《SharePoint 2010 应用程序开发指南》第二章预览
    SharePoint 2013 App 开发 (1) 什么是SharePoint App?
    使用Jscex增强SharePoint 2010 JavaScript Client Object Model (JSOM)
    搜索范围的管理
    SharePoint 2010 服务应用程序(Service Application)架构(1)
    SharePoint 2010 服务应用程序(Service Application)架构(2)
    SharePoint 2013 App 开发 (2) 建立开发环境
  • 原文地址:https://www.cnblogs.com/masonlu/p/7526079.html
Copyright © 2011-2022 走看看