ASP.NET / Mono MVC4 Web API v.1 application.
How to catch calls to undefined api methods.
Calling http://localhost:52216/erp/api/undefinedmethod
returns error to browser:
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http:// localhost:52216/erp/api/undefinedmethod'.
</Message>
<MessageDetail>
No type was found that matches the controller named 'undefinedmethod'.
</MessageDetail>
</Error>
How to catch this error for logging to database? I tried code from question
How do I log ALL exceptions globally for a C# MVC4 WebAPI app?
but Application_Error and exception filter code is still not executed, error is returned to browser. How to catch this error ?
If url is without api like
'http://localhost:52216/erp/undefinedmethod'
Application_Error is executed properly.
WebAPI configuration from VS2013 WebAPI project template is used:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Update
I tried changed answer. API controllers are using Forms authorication and are decorated with standard [Authorize] attribute. If authorization fails, standard xml api error message
<Error>
<Message>Authorization has been denied for this request.</Message>
</Error>
occurs. How to catch this error also ?