Application_Error Jump to custom error page in ASP. Global.asax

Source: Internet
Author: User

When we developed the ASP, we often wanted to jump to a custom error page after a system error, so we usually application_ in global.asax. The error method uses the Response.Redirect method to jump to a custom error page, but sometimes (especially when the site is deployed to IIS) Application_ The use of the Response.Redirect method in the error method is invalidated, and the default error yellow pages are displayed when an exception error occurs for ASP. The root cause is that although we have used the Response.Redirect method in the Application_Error method, when the system has an exception error, ASP. NET does not think the exception is handled, so ASP. NET will not jump to Application_ The page that Response.Redirect points to in the error method will eventually jump to the default error yellow Pages of ASP. The solution to this problem is simply to call Server.ClearError () before jumping using Response.Redirect in the Application_Error method. The method tells the ASP that an exception error has been processed so that the Response.Redirect method will jump to the custom error page.

Here is a sample code:

1 usingSystem;2 usingsystem.web;3 usingSYSTEM.WEB.MVC;4 usingSystem.Web.Routing;5 usingSystem.Web.Http;6 7 namespaceRedirecttoerrorpage8 {9      Public classglobal:httpapplicationTen     { One         voidApplication_Start (Objectsender, EventArgs e) A         { -             //Code that runs on application startup - Arearegistration.registerallareas (); the globalconfiguration.configure (webapiconfig.register); - routeconfig.registerroutes (routetable.routes);  -         } -  +         //despite our application_ in Global.asax, The error method uses the Response.Redirect method for page redirection, but when an error occurs on the system, ASP. NET considers that the error is not handled, so the final page will be redirected to ASP. NET default error yellow Pages without jumping to our application The page that is pointed to by Response.Redirect in the _error method.  -         protected voidApplication_Error (Objectsender, EventArgs e) +         { AServer.ClearError ();//calling the Server.ClearError method in Global.asax is equivalent to telling the ASP to throw an exception that has already been processed and does not require the system to jump to the error yellow Pages of the ASP. If you want to global.asax out of the ClearError method you can use the HttpContext.Current.ApplicationInstance.Server.ClearError ().  atResponse.Redirect ("~/errorpage.html",true);//call the Server.ClearError method and then call Response.Redirect to successfully jump to the custom error page. -         } -}

Application_Error Jump to custom error page in ASP. Global.asax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.