如標題中所述,我需要在我的網站(使用 ASP.NET 制作)中為 404 錯誤配置自定義網頁并托管在 IIS 上。
為此,我將 web.config 配置如下:
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/mycustompath/404.html" responseMode="ExecuteURL" />
</httpErrors>
一切正常,如果我輸入:
https://mydomain/path/page.aspx__somethingwrong__
我正確地轉到我的自定義 404.html 如果我放一些隨機字符而不是檔案,也會發生同樣的情況
但是,如果我弄亂了路徑,或者鍵入了一個不存在的 .aspx 檔案,那么 IIS 會將我重定向到默認的 404 頁面。我不清楚為什么。
無效的網址示例:
https://mydomain/invalid_path/page.aspx
https://mydomain/path/fictional_page.aspx
uj5u.com熱心網友回復:
您可以在 web.config 檔案中添加errorMode="Custom"和existingResponse="Replace"來解決您的問題。
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/MyErrorPages/404.html" responseMode="ExecuteURL" />
</httpErrors>
existingResponse 指定當 HTTP 狀態碼是錯誤時對現有回應的處理,它還有 3 個選項:
- auto(默認):僅當設定了 SetStatus 標志時才保持回應不變。
- Replace :即使設定了 SetStatus 標志,也替換現有回應。(意味著無論如何都要使用自定義頁面)
- Passthrough :如果存在現有回應,則保持回應不變。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/532856.html
