ASP.NET Web API - ActionFilter property not set

I have a base controller class that extends from ApiController

and defines this abstract method:

[HttpPost]
[ValidateModelFilter(Argument = "dto")]
public abstract Task<IHttpActionResult> Post(PostDTO dto);

      

This method will be implemented by all child classes, but the filter will be enforced without implementing them.

This approach works great, my filter is complete. But my property is Argument

not set to "dto"

.

This is my class ValidateModelFilter

:

public class ValidateModelFilter : ActionFilterAttribute, IActionFilter, IFilter
{
    public string Argument { get; set; }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
         //Some validation here...
    }
}

      

Everything works fine except that my property is Argument

always null

if I apply the filter like this [ValidateModelFilter]

or this[ValidateModelFilter(Argument = "dto")]

Also note that I am currently using a conveyor OWIN

and Ninject

for dependency injection.

+3


source to share


1 answer


For future readers, I managed to solve the problem.

I have registered my filter in my application config:

public static void Register(HttpConfiguration configuration)
{
    configuration.Filters.Add(new ValidateModelFilter());
}

      



This makes it "Global" and is always executed whether you have an annotation for the filter or not.

I commented out this line and now my filter is working fine.

//configuation.Filters.Add(new ValidateModelFilter());

      

+2


source







All Articles