2

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?

BCarlson
  • 1,122
  • 2
  • 11
  • 26

1 Answers1

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, let me give this a try. I think I understand the plumbing. – BCarlson Sep 23 '13 at 20:53
  • 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