MVC 4 Web API register filter
Asked Answered
H

1

10

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

Heffernan answered 20/2, 2013 at 14:17 Comment(0)
M
10

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.

Mccullers answered 20/2, 2013 at 14:22 Comment(3)
Ok, this solution allowed my project to build and I have my filter being registered to both "System.Web.Http.Filters.HttpFilterCollection" for the Web API as well as "GlobalFilterCollection" for standard MVC action filtering. However, when I navigate to one of the api methods, the filter is not being executed. The filter is working for the standard MVC controller actions, but not for the Web API methods. Any suggestions?Heffernan
Make a new question. Don't ask new questions in comments.Leap
@Sam, Why do you clear the filter collection before you add your own?Dees

© 2022 - 2024 — McMap. All rights reserved.