简体   繁体   中英

Return correct http error status code to browser

In my global.asax I have written code into the Application_Error event which catches all errors and shows a generic error page to the user as well as logging/emailing the error that occurred.

However, I have realised that the error page does not return the correct status code to the browser which has meant that, to a service like UptimeRobot, the site is still seen as functioning even when the page is broken.

Is it possible to derive the correct status code from an error and return it to the browser?

By default IIS intercepts the status code and sends either the debug info or a custom error page. You can deactivate this default behavior and manually set the error code through the Response object:

Context.Response.TrySkipIisCustomErrors = true;  
Context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Context.Response.ContentType = "text/plain";
Context.Response.Write("Your error message");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM