Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 4 Web API register filter

I am using MVC 4 Web API to create a service layer for an application. I am trying to create a global filter that will act on all incoming requests to the API. Now I understand that this has to be configured differently than standard MVC global action filters. But I'm having issues getting any of the examples I'm finding online to work.

The problem I am running into is in registering the filter with Web API.

I have my Global.asax set up like this...

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        MVCConfig.RegisterRoutes(RouteTable.Routes);
        MVCConfig.RegisterGlobalFilters(GlobalFilters.Filters);

        WebApiConfig.RegisterRoutes(GlobalConfiguration.Configuration);
        WebApiConfig.RegisterGlobalFilters(GlobalConfiguration.Configuration.Filters);
    }
}

My standard Mvc routing and filters work correctly. As does my WebApi routing. Here is what I have for my webApi filter registration...

public static void RegisterGlobalFilters(System.Web.Http.Filters.HttpFilterCollection filters)
{
    filters.Add(new PerformanceTestFilter());
}

And here is the PerformanceTestFilter...

public class PerformanceTestFilter : ActionFilterAttribute
{
    private readonly Stopwatch _stopWatch = new Stopwatch();

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _stopWatch.Reset();
        _stopWatch.Start();
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        _stopWatch.Stop();
        var executionTime = _stopWatch.ElapsedMilliseconds;
        // Do something with the executionTime
    }
}

This filter works fine when it is registered with the standard Mvc GlobalFilterCollection, but when I try to register it with System.Web.Http.Filters.HttpFilterCollection I get an error saying that it is not assignable to parameter type System.Web.Http.Filters.IFilter.

So I'm assuming that my PerformanceTestFilter needs to inherit from something other than ActionFilterAttribute in order to be registered as a webapi filter. I'm just not sure what that needs to be.

I imagine I will need to create two individual filters to work with mvc and webapi respectively. If there is a way to create a filter that could be registered to both, that would be great. But my primary concern is simply to get it working for webapi.

Thanks

like image 582
jdavis Avatar asked Feb 20 '13 14:02

jdavis


1 Answers

The following should work. We actually use this for our web API project.

GlobalConfiguration.Configuration.Filters is of type HttpFilterCollection

 var filters = System.Web.Http.GlobalConfiguration.Configuration.Filters;
 filters.Clear();
 filters.Add(new ValidationActionFilterAttribute());

 public class ValidationActionFilterAttribute : FilterAttribute, IActionFilter, IFilter
 {
    ...
 }

Also, if you're working in a project that contains both MVC and WebAPI assembilies, could you check what's the namespace your ActionFilterAttribute's namespace. It's fairly confusing cause there are two ActionFilterAttributes under both:

  System.Web.Http.Filters
   System.Web.Http.Mvc

Source: Why is my ASP.NET Web API ActionFilterAttribute OnActionExecuting not firing?

It appears that you will need to have two filters, one for API and one for MVC. You can factor the common code into a separate class, and then just use the specific filter to call through to your common class, thus not violating DRY and essentially using the actual filters as wrappers which can be registered as filters.

like image 86
Sam Shiles Avatar answered Oct 08 '22 02:10

Sam Shiles