I have the followin attribute.
I need to do:
- return a message to the browser in the
filterContext.Result
(I suppose it's that place to do it) - be able to stop an action being executed
but, after start the debuggind process, the debugger steps inside OnActionExecuting
method, and I see an error instead of my page. I thought it's gonna be executed before the SearchItems
method - why that happens ?
public class MyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
bool isValid = false; //some logic here
if (!isValid)
{
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.PaymentRequired;
filterContext.Result = new EmptyResult();
return;
}
base.OnActionExecuting(filterContext);
}
}
global.asax
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new MyAttribute());
filters.Add(new HandleErrorAttribute());
}
controller:
public class MainApiController : ApiController
{
[MyAttribute]
[HttpPost]
public HttpResponseMessage SearchItems()
{
...
}
}