HttpResponseException in .NET Core - HTTP response codes

C#.NET Core

Please contribute by voting. Thanks!

Do you use .NET Core WebAPI and are looking for the equivalent to HttpResponseException? Or perhaps looking for a way to throw an exception to respond with different HTTP status codes.

Unfortunately that is by default not possible in dot net core. According to this excellent answer on StackOverflow this behavior is intentional, as what you actually are doing is controlling more or less normal program flow with Exceptions - and that is a bad practice.

My recommendation is that you stick with the standard IActionResult return type and the built in methods like Ok(), BadRequest(), etc.

Or if you are developing a larger app - add your own means to facilitate the behavior - but preferably without resorting to exceptions for unexceptional events:

Article created: Apr 21 '20. Edited Apr 21 '20.

Your comment

You need to sign up / log in to comment this article


Created by Matt Hamilton [13] Apr 21 '20

Share article

Do you know about


Write an article