web.config中customErrors與httpErrors的區別

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

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

Error Pages指向/NotFound/SystemWebServer404.htmlweb

設定結果會反應在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,故得證。url

補充一點:httpErrors有個errorMode屬性,預設爲DetailedLocalOnly,至關於customErrors mode="RemoteOnly",故在本機測試看不到自訂錯誤頁,要改爲Custom纔看獲得。這是IIS 7起加入的行爲,還停在IIS 6的腦殼沒意識到有差別,花了點時間才搞定。spa

相關文章
相關標籤/搜索