Question

I am migrating a project that was developed using WebApi Preview 5 (when it was part of WCF) to the final release of WebApi (part of MVC4). There is a document describing the process but it is extremely simplistic and doesn't cover most of the issues.

Now one of the issues I am facing is that a GlobalErrorHandler was created by inheriting from HttpErrorHandler and then overriding OnTryProvideResponse and that was used to hook error handling with Elmah. Now that was registered on AppStart with a line like this:

var configuration = new WebApiConfiguration();
//some other configuration for security and CreateInstance
configuration.ErrorHandlers = 
(handlers, endpoint, description) => handlers.Add(new GlobalErrorHandler())
};
//then some registration
RouteTable.Routes.MapServiceRoute<SomeObject>("routeName", configuration);

and then mapping different route to this configuration. All this code doesn't work in the new world of MVC4 WebApi, it seems like there is a conflict between HttpErrorHandler and it can't even implement its members properly.

Now I've seen general posts about how to register Elmah with WebApi but I am trying to stick to the original code as much as possible and I am assuming - may be I am wrong - that there is a direct equivalent to what Microsoft had in the Preview version and what they released in the final one. So my questions:

  1. What is the equivalent of this Global Error handling registation in ASP.NET MVC4 WebApi?
  2. Do I need to do the configuration the same way it is done here (default webapi samples project doesn't seem to have similar code)
  3. What is the equivalent of that route registration line of code: RouteTable.Routes.MapServiceRoute("routeName", configuration);
Was it helpful?

Solution

If you create a quick one-off WebApi MVC project in Visual Studio you will see an App_Start folder which contains some classes which have static methods for handling the registration, specifically:

  • FilterConfig.cs
  • WebApiConfig.cs

WebApi Config is where you need to register routes etc...

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

Filter config is what you need to handle your global errors... Filter config has a default error handler attribute added which you can swap out or out

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }
}

Global.asax calls these static registration scripts like so:

 protected void Application_Start()
    {

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

    }

In regard to Elmah it appears simplying including the Nuget package will register it...

Look for the package Elmah.Mvc

PM> Install-Package Elmah.MVC

it used to be like this How to get ELMAH to work with ASP.NET MVC [HandleError] attribute? but now according to this blog it has changed:

HandleErrorAttribute inside If you tried to use ELMAH in ASP.NET MVC, you are probably implemented your own HandleErrorAttribute, as it's shown in this example. You no longer need to apply this custom code with Elmah.MVC. As soon you installed package, so can safely remove your HandleError attribute, since it's already included into package.

This now appears to register itself in the Web.Config as a managedHandler so your code does not need to reference Elmah directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top