Hashem Aboonajmi
Hashem Aboonajmi

Reputation: 13890

adopting action filters for both MVC and web Api controllers

as you know MVC and web api action filters uses different namespaces for their filters:

in web api:

public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)

HttpActionExecutedContext is in system.web.http.filters

in MVC:

public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)

ActionExecutedContext is in system.web.mvc

I want to have one filters for all of my controllers whenever web api or MVC.

so how can I solve this issue?

Upvotes: 5

Views: 1668

Answers (1)

MVC and Web API are different frameworks and where they converge is the hosting part. At this point in time, MVC and Web API can be web-hosted (IIS + ASP.NET pipeline) and hence you can write an IIS module to do what you want.

OWIN middleware could be a better option but unfortunately MVC cannot be OWIN-hosted at this point. However, you can use a stage marker to run an OWIN middleware in ASP.NET pipeline at the stage you want but as far as I can see, PipelineStage enum has PreHandlerExecute as the last stage. This means you can do something like OnActionExecuting but not OnActionExecuted.

Upvotes: 1

Related Questions