C
C#9mo ago
Sander

What is the best way to return errors in api controllers?

What is the best way to return errors in asp.net core api controllers? i currently use
return Problem("Some error message", statusCode: 400);
return Problem("Some error message", statusCode: 400);
But are the better ways to do it?
3 Replies
Omnissiah
Omnissiah9mo ago
for some statuses there are already premade methods (like there is one for Ok()) but apart from that, it depends how they are being used
Sander
SanderOP9mo ago
im not taling about http status code but rather just errors in general. lets say i got a endpoint for registering a user. but it first checks if the username isnt already taken. how do i best return that error in case the username is already taken?
ngtphat.towa
ngtphat.towa9mo ago
Handle errors in ASP.NET Core
Discover how to handle errors in ASP.NET Core apps.
Handle errors in ASP.NET Core web APIs
Learn about error handling with ASP.NET Core web APIs.
Marinko Spasojević
Code Maze
Global Error Handling in ASP.NET Core Web API - Code Maze
Find out how to replace try-catch blocks in your code with the Global Error Handling by using the custom or built-in middleware in ASP.NET Core.
Want results from more Discord servers?
Add your server