Learn to handle runtime errors using exceptions and return appropriate error codes and error messages in ASP.Net Web API
Microsoft’s ASP.Net Web API is a lightweight framework you can use to build stateless RESTful services that run on HTTP. Exceptions are errors that occur at runtime, and exception handling is the technique of handling runtime errors in your application code.
Every ASP.Net Web API developer should know how to handle exceptions in Web API and how to send out appropriate error codes and error messages from Web API controller methods. We will examine how to perform these tasks in the sections below.
Using HttpResponseException in ASP.Net Web API
You can use the HttpResponseException class to return specific HTTP status codes and messages from your controller methods in Web API.
If your Web API returns IHttpActionResult, you might want to write the GetEmployee method
Note that the error code and error message are assigned to the response object, and that an instance of HttpResponseException is returned when an exception occurs in the action method of the Web API controller.
Using HttpError in ASP.Net Web API
You can use the CreateErrorResponse extension method in your Web API controller method to return meaningful error codes and error messages. Note that the CreateErrorResponse method creates an HttpError object and then wraps it inside an HttpResponseMessage object.
Refer to the GetEmployee() method shown above. This method accepts an employee ID as a parameter and uses this ID to search and retrieve an employee record using the employee repository instance. If an employee record having the specified employee ID is not found, an instance of HttpResponseException is thrown. Note how the appropriate error message and error code are constructed before the exception instance is thrown from the Web API controller method.
Using exception filters in ASP.Net Web API
Exception filters are filters that can be used to handle unhandled exceptions that are generated in your Web API controller methods. In other words, you can use exception filters to catch unhandled exceptions in Web API that originate from your controller methods. Note that a global error filter is a good approach to handling exceptions in your Web API if unhandled exceptions are thrown and not handled in your controller methods.
To create an exception filter, you need to implement the IExceptionFilter interface. You can also create exception filters by extending the abstract class ExceptionFilterAttribute and then overriding the OnException method. Note that the ExceptionFilterAttribute abstract class in turn implements the IExceptionFilter interface.
You can register your exception filters in one of the following three ways:
- At the action level
- At the controller level
ASP.Net Web API supports the use of HttpResponseException to handle exceptions both at the controller level and at the action level. When an action method in Web API throws an uncaught exception, the exception is translated to HTTP Status Code 500, i.e., “Internal Server Error.” If you use HttpResponseException, you can specify the status code you would like to return in the constructor of the HttpResponseException class. In this way you can customize your error codes to make them more meaningful.