Posts

Showing posts from January 1, 2023

Handle errors in ASP.NET Core web APIs

Image
Handle errors in ASP.NET Core web APIs Exception handling is one of the critical areas in modern web application development. If exceptions are not handled properly, the whole app can be terminated, causing severe issues for users and developers. Try-catch blocks are widely used in apps to handle exceptions. They are the most basic way of handling exceptions. ​ The above example is a typical case where we use try-catch block to handle the exception. The try-catch block method is ideal for novice developers, and it is something that every developer should be aware of. However, this technique has a disadvantage when working with massive projects with complex architectures. In such cases we have to use Exception Handling Middleware to handle the exceptions. Exception handler In Program.cs, call UseExceptionHandler to add the Exception Handling Middleware: ​ Configure a controller action to respond to the /error route: ​ Add [ApiExplorerSettings] attribute and set its IgnoreApi property t