Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9320313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:36:17+00:00 2026-06-19T03:36:17+00:00

I am using MVC 4 Web API to create a service layer for an

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:36:19+00:00Added an answer on June 19, 2026 at 3:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using extjs mvc style to create a web application. I don't have any
Background I am coding an API layer in C# using MVC 4 Web API
I have an ASP MVC Web Api project that outputs json using json.net. I
I am trying to create a API based service using Symfony2 which is the
I am developing some sort of service using asp.net mvc 4 web api. On
I'm using MVC 4 web api that comes with visual studio 2012. As I've
Trying to set up a new site using MVC RC4 web API in Visual
I am using MVC with WEB API in my application. I am using Kendo
I am using mvc webapi to create a REST API and struggling to find
I'm using a ExtJS DateField control in an ASP.NET MVC web application. I've set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.