I am trying to use a filter on my WebAPI project, but for some reason the OnActionExecuting(HttpActionContext actionContext)
method doesn't fire.
This is the filter class (taken from this question and modified slightly):
using System.Net;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
public class MyNoActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (IfDisabledLogic(actionContext))
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.NotFound);
}
else
base.OnActionExecuting(actionContext);
}
private bool IfDisabledLogic(HttpActionContext actionContext)
{
return false;
}
}
I added the filter attribute to my controller:
[Route("api/[controller]")]
[ApiController]
[MyNoActionFilter]
public class ExampleController : ControllerBase
{...}
And I also added it to my Program.cs file:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddScoped(typeof(MyNoActionFilterAttribute));
...
I tried setting a breakpoint in the OnActionExecuting function but it never seems to be reached.
What am I missing here?