All you need is to assign the Response:
public class MyActionFilterAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
actionContext.Response = actionContext.Request.CreateResponse(
HttpStatusCode.OK,
new { foo = "bar" },
actionContext.ControllerContext.Configuration.Formatters.JsonFormatter
);
}
}
Assuming the following controller action:
[MyActionFilter]
public string Get()
{
return "OK";
}
this custom action filter will short-circuit the execution of the action and directly return the response we provided.