Its been a while I haven’t blogged for sometime. To create an action filter in Web Api 2 and return the response directly you can use the code below
public class StepUpAttribute : ActionFilterAttribute
{
public IUserFactory CurrentUser { get; set; }
public override void OnActionExecuting(HttpActionContext filterContext)
{
if (CurrentUser.IsSteppedUp)
return;
// stop request process and return
filterContext.Response = filterContext.Request.CreateResponse(HttpStatusCode.OK,
ErrorResponseFactory.FromUnsafeActionDetectedResponse(),
filterContext.ControllerContext.Configuration.Formatters.JsonFormatter);
}
}
Leave a Reply