AgentFire
AgentFire

Reputation: 9780

Detect the end of request on ApiController

I am developing a web application with MVC4 and found pretty useful this method:

class Controller
{
    protected virtual void OnActionExecuted(ActionExecutedContext filterContext);
}

Which allows me to call UnitOfWork.SubmitChanges(); or Transaction.Rollback(); or other good stuff which needs to be called only after the request is processed and the changes to database are pending.

When I started to work with ApiController I did not found that useful method.

How can I detect the end of http requests on my web-api controllers?

Upvotes: 1

Views: 1305

Answers (1)

Rob
Rob

Reputation: 4947

You can make a ActionFilterAttribute for the ApiController.

 public class JsonpAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            //Logic
        }
    }

And in your controller:

[Jsonp]
public class testcontroller : ApiController

Note that Web Api uses System.Web.Http.Filters whereas MVC4 uses System.Web.Mvc

Upvotes: 3

Related Questions