HI mọi người, mình không thể Custom 404 error is not redirecting to Global.asax in MVC C# được, vì một số link nó không redirect về tập tin Global.asax Application_Error mà nó ra trang 404 mặc định của IIS. Mình muốn tất cả các all Requests phải được trả về redirect to Global.asax lệnh Application_Error thì làm như thế nào. Vì mình muốn 301 các liên kết lỗi về các dường dẫn chỉ định theo ý muốn ý. Mà hiện tại mình vào các liên kết có đuôi .html VD: /lien-ket/demo.hml thì nó không trùy cập vào Application_Error, còn mnfh bỏ /lien-ket/demo thì nó lại truy cập vào được. Bạn nào biết cách redirect tất cả các liên kết lỗi Requests 404 về Application_Error chỉ mình với, cảm ơn Mã: protected void Application_Error(Object sender, EventArgs e) { Exception exception = Server.GetLastError(); // Clear the error Response.Clear(); Server.ClearError(); if (exception is HttpException) { HttpException ex = exception as HttpException; if (ex.GetHttpCode() == 404) { var url = HttpContext.Current.Request.Path.TrimEnd('/'); var redirect = ..............Xử lý lấy nội dung cần redirect qua if (redirect.Keys.Contains(url)) { // 301 it to the new url Response.RedirectPermanent(redirect[url]); } else { // 301 it to home nếu không có url nào trùng thì redirect vào trang chủ Response.RedirectPermanent("/"); } } } }
Mặc định RouteConfig của web MVC sẽ không cho bạn redirect các trang lỗi dạng có đuôi .html hay .abc,... về Application_Error ha bạn. Để kích hoạt thông báo cho Server biết bạn chấp nhận mọi kết nối dù là lỗi hay sai cấu trúc RouteConfig đều phải trả về Application_Error thì bạn vào web.config của web thêm dòng code sau nhé: Mã: <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer>