I'm using WebApi for a rest service. I need to have every request hit a inbound filter/action (to price the request) and hit a outbound filter/action (to bill the results). About a month ago I came across the term but I can't see to recall it. Something like "PostAction" or "PostFilter". Can anyone point me in the correct direction?
Asked
Active
Viewed 1,231 times
2
-
you might be referring to `RegisterGlobalFilters` ? – Christian Phillips Sep 23 '13 at 19:58
-
Searching on that, let's see. – BCarlson Sep 23 '13 at 20:09
-
Still looking at RegisterGlobalFilters, but I need to execute code before the controller executes, and after the controller executes. I haven't see how RegisterGlobalFilters can be ran before and after controller. Still looking though. – BCarlson Sep 23 '13 at 20:16
-
Take a look here, this may help http://msdn.microsoft.com/en-us/library/gg416513(v=vs.98).aspx – Christian Phillips Sep 23 '13 at 20:21
-
That's looking good. I'm trying some prototype code to test it out. – BCarlson Sep 23 '13 at 20:32
1 Answers
1
If you take a look at this link Filtering in ASP.NET MVC, they have an example of how to apply a custom filter attribute
to a controller, which is applied to all Action
methods.
Here's an example of the code from the link...
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
}
You can see the custom filter added to the controller.
In your scenario, the IActionFilter may be of use, which has two OnActionExecuted
& OnActionExecuting
.
Take a look at How to add global ASP.Net Web Api Filters? for help with WebAPI.

Community
- 1
- 1

Christian Phillips
- 18,399
- 8
- 53
- 82
-
I don't think it will work. For a controller to work in WebApi, I have to derive from ApiController. When I derive from Controller, WebApi is breaking. When I switch it back to ApiController, WebApi starts working. At least based upon my prototype. – BCarlson Sep 23 '13 at 20:46
-
-
Ok got it working. `ActionFilterAttribute` was what I used to derive from and implemented `OnActionExecuted` and `OnActionExecuting`. Reference `System.Web.Http.Filters` and NOT `System.Web.Mvc` – BCarlson Sep 24 '13 at 14:35