Introduction
In this article, we will define exception handling in the ASP.NET Web API.
Excepting handling is a process that handles an exception by saving the current state of execution and switches the execution to the specific subroutine. The following are used:
- HttpResponseException
- Exception Filter
- Registering Exception Filters
- HttpError
HttpResponseException
It is a special type of exception. It throws an HTTP code as an exception that is defined in the exception constructor. In it the exception is converted into a HTTP response.
- public Detail GetDetail(int Id)
- {
- Detail item = reposite.Get(Id);
- if (val == null)
- {
- throw new HttpResponseException(HttpStatusCode.NotFound);
- }
- return val;
- }
If there is an Id parameter not defined then it will return the response "NotFound".
We can create own response message for controlling the resoponse, and this message wille be included with the HttpResponseException.
- public Detail GetDetail(int Id)
- {
- Detail val = reposite.Get(Id);
- if (item == null)
- {
- var show = new HttpResponseMessage(HttpStatusCode.NotFound)
- {
- Con = new StringContent(string.Format("There is no Detail with Id {0}", Id)),
- Reason_Phrase = "Detail Id not defined"
- }
- throw new HttpResponseException(Show);
- }
- return val;
- }
Exception Filters:
If there is any Unhandled type exception generated that is not the HttpResponseException type Exception then the Exception filter is used to handle these exceptions. It is written using the Web API.
The "System.Web.Http.Filters.IExceptionFilter" interface and "System.Web.Http.ExceptionFilterAttribute" class are used.
- namespace hello.Filters
- {
- using System;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http.Filters;
- public class E_Filter : ExceptionFilterAttribute
- {
- public override void OnException(HttpActionExecutedContext cont)
- {
- if (cont.Exception is NotImplementedException)
- {
- cont.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented);
- }
- }
- }
- }
Registering Exception Filters
We register the exception in various ways that are as follows:
-
Globally
-
By Action
-
By Controller
Globally
When the exception is registered globally, first we need to create an instance of the GlobalConfiguration.Configuration.Filters.
- GlobalConfiguration.Configuration.Filters.Add(new hello.E_Filter());
If we are working on ASP.NET MVC 4 Application then we add the API configuration code in WebApiConfig.cs class that is located in the App_Start folder.
- public static class WebApiConfig
- {
- public static void Register(HttpConfiguration config)
- {
- config.Filters.Add(new hello.E_Filter());
- }
- }
By Action:
We can also apply a filter for the specific action.
- public class DetailController : ApiController
- {
- [NotImplExceptionFilter]
- public contact GetContact(int id)
- {
- throw new NotImplementedException("method can not be implement");
- }
- }
By Controller
We can apply the filter on the all method of a controller.
- [NotImplExceptionFilter]
- public class DetailController : ApiController
- {
- }
HttpError
HttpError provides a way to show the error message.
- public class DetailController : ApiController
- {
- public HttpResponseMessage GetDetail(int Id)
- {
- Detail val = reposite.Get(Id);
- if (val == null)
- {
- var report = string.Format("Id of Detail = {0} ",Id);
- HttpError error = new HttpError(report);
- return Request.CreateResponse(HttpStatusCode.NotFound, error);
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.OK, val);
- }
- }
- }
We can also use the CreateErrorResponse instead of HttpError.
Let's see an example of CreateErrorResponse:
- public class DetailController : ApiController
- {
- public HttpResponseMessage GetDetail(int Id)
- {
- Detail val = reposite.Get(Id);
- if (val == null)
- {
- var report = string.Format("Id of Detail = {0} ",Id);
- return Request.CreateErrorResponse(HttpStatusCode.NotFound, error);
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.OK, val);
- }
- }
- }
This is the description of exception handling.