HttpResponseException in .NET Core - HTTP response codes

C#.NET Core

Please contribute by voting. Thanks!
1

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 https://stackoverflow.com/a/47142207/4347240 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: https://stackoverflow.com/questions/1504302/is-it-a-good-or-bad-idea-throwing-exceptions-when-validating-data

Article created: Apr 21. Edited Apr 21.

Your comment

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

Author

Created by Matt Hamilton [13] Apr 21

Share article

Do you know about

JSON?

Write an article