自定義了錯誤頁面,但IIS卻顯示http服務器內部500錯誤的解決辦法
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
1. 激活異常過濾器 當自定義異常被捕獲時,異常過濾器變為可用。為了能夠獲得自定義異常,打開Web.config文件,在System.Web.Section下方添加自定義錯誤信息。 <system.web> <customErrors mode="On"></customErrors> 3. 綁定異常過濾器 將過濾器綁定到action方法或controller上,不需要手動執行,打開 App_Start folder文件夾中的 FilterConfig.cs文件。在 RegisterGlobalFilters 方法中會看到 public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); //filters.Add(new AuthorizeAttribute()); } } 如果需要刪除全局過濾器,那么會將過濾器綁定到action 或controller層,如下: [AdminFilter] [HandleError] public async Task<ActionResult> Upload(FileUploadViewModel model) @model HandleErrorInfo @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="viewport" content="width=device-width" /> <title>錯誤</title> </head> <body> <hgroup> <h1>錯誤。</h1> <h2>處理你的請求時出錯。</h2> </hgroup> Error Message :@Model.Exception.Message<br /> Controller: @Model.ControllerName<br /> Action: @Model.ActionName </body> </html> 5. 運行后故意出錯,顯示的卻是http服務器內部500錯誤,并不顯示Error中的自定義錯誤信息。解決辦法: 轉載? 該文章在 2025/8/22 15:22:40 編輯過 |
關鍵字查詢
相關文章
正在查詢... |