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

I want to simulate this error so I can check a generic error page is displayed, not the HTTP 500 one, in light of the recent security vulnerability.

We include special processing in the site itself for 404 and 403 so I want to make sure that errors without special processing work too.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

throw new Exception();

This will generate a HTTP 500

Method 2

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!

Method 3

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).

Method 4

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.

Method 5

you can break the web.config file. Put a malformed tag for tests

Method 6

This generate a custom http error code in classic asp.

<%@language=Jscript%>

<%
    Response.Status = "996 TerraNova GeoWeb Internal Server Error";
    Response.End;
%>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x