We can register a Web API exception filter:
- By action
- By controller
- Globally
To apply the exception filter to a controller action method:
public class EmployeeController : ApiController
{
[]
public Address GetAddress(int id)
{
throw new NotImplementedException("This method is not implemented");
}
}
To apply the exception filter to a controller class:
[]
public class EmployeeController : ApiController
{
public Address GetAddress(int id)
{
throw new NotImplementedException("This method is not implemented");
}
}
To apply the exception filter globally to all Web API controllers:
public static class WebApiConfig
{ public static void Register(HttpConfiguration config) { // Web API configuration and services // Web API routes config.Filters.Add(new EmployeeDemoProject.NotImplExceptionFilterAttribute()); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } }
No comments:
Post a Comment