Returning exceptions as JSON messages
I am developing an API with ASP.NET Core and I am struggling with the exception handling. When any exception occurs, or in any controller where I want to return custom errors with different status codes, I want to return JSON-formatted exception reports. I do not need an HTML in the error responses. I'm not sure if I should use middleware for this, or something else. How should I return JSON exceptions in an ASP.NET Core API? An exception filter (either as an attribute, or a global filter) is what you are looking for. From the docs : Exception filters handle unhandled exceptions, including