HttpResponseException cannot handle all type of errors. It is only designed for handling specifically for returning an HTTP response. An exception filter is executed when a controller method throws any unhandled exception that is not an HttpResponseException exception.
Acton Filters are present in System.Web.Http.Filters.IExceptionFilter interface and we can override the OnException method of System.Web.Http.Filters.ExceptionFilterAttribute class and put our own implementations.
The Response property of the HttpActionExecutedContext object contains the HTTP response message that will be sent to the client. Below is the example:
namespace EmployeeDemo.Filters { using System; using System.Net; using System.Net.Http; using System.Web.Http.Filters; public class NotImplExceptionFilterAttribute : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext context) { if (context.Exception is NotImplementedException) { context.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented); } } } }
Acton Filters are present in System.Web.Http.Filters.IExceptionFilter interface and we can override the OnException method of System.Web.Http.Filters.ExceptionFilterAttribute class and put our own implementations.
The Response property of the HttpActionExecutedContext object contains the HTTP response message that will be sent to the client. Below is the example:
namespace EmployeeDemo.Filters { using System; using System.Net; using System.Net.Http; using System.Web.Http.Filters; public class NotImplExceptionFilterAttribute : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext context) { if (context.Exception is NotImplementedException) { context.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented); } } } }
No comments:
Post a Comment