Aug282012

Return a 500 Error in ASP.NET MVC

A good question came up today that I couldn't answer immediately: how do you return a 500 Server Error (or any other HTTP Status Code) from ASP.NET MVC?

Thankfully, it's extremely simple; all you have to do is return a new HttpStatusCodeResult with whatever code you'd like:

return new HttpStatusCodeResult(500);

"500" can be replaced with whatever HTTP Status Code you'd like.

Reference: HttpStatusCodeResult Class on MSDN