How to simulate an HTTP 500 error on my ASP.NET app?

Here's a way to do this without modifying your site in any way:

  1. From your web browser, open a page on your site that has a postback form.
  2. Press F12 to open developer tools.
  3. From the HTML tab, search for __VIEWSTATE and change the value in any way.
  4. Post to the form

This will cause a "Validation of viewstate MAC failed" ASP.Net Exception, which returns a 500 internal server error HTTP response code.

Breaking the web.config with a malformed tag also works, but defeats the purpose if you are trying to test some settings in your web.config (like Failed Request Tracing).


Change the name of your dll file. It will crash the app if you ask for a route afterwards because it won't find the controller. I used this to test my logging.


throw new Exception();

This will generate a HTTP 500


I think you can do this by overriding page init and adding the 500 status code to the response like the following:

protected void Page_Init(object sender, EventArgs e)
{
    Response.Clear();
    Response.StatusCode = 500;
    Response.End(); 
}

Enjoy!

Tags:

Asp.Net