Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch undefined api method calls in ASP.NET MVC4 Web API

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 ?

like image 222
Andrus Avatar asked Nov 26 '13 09:11

Andrus


1 Answers

Imran Baloch wrote an article on exactly how to achieve this. Basically you need to create your own HttpControllerSelector and HttpActionSelector. You can find the article here.

EDIT:

If your application uses routes other than those registered in the WebApiConfig you will need to make some changes to the routing. Instead of defining the Error404 route at the end of the Register method, define a new method (RegisterNotFound) to register the route:

public static class WebApiConfig
{    
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

    public static void RegisterNotFound(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "Error404",
            routeTemplate: "{*url}",
            defaults: new { controller = "Error", action = "Handle404" }
        );
    }
}

And then in the Global.asax register call this method last:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    WebApiConfig.RegisterNotFound(GlobalConfiguration.Configuration);


    GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), 
        new HttpNotFoundAwareDefaultHttpControllerSelector(GlobalConfiguration.Configuration));
    GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpActionSelector), 
        new HttpNotFoundAwareControllerActionSelector());

}
like image 75
Jon Susiak Avatar answered Sep 23 '22 22:09

Jon Susiak