简体   繁体   English

ASP.NET Web API - 未设置ActionFilter属性

[英]ASP.NET Web API - ActionFilter Property not being set

I have a base controller class that extends from ApiController and specifies this abstract method: 我有一个从ApiController扩展的基本控制器类,并指定这个抽象方法:

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

This method will be implemented by all the child classes, but the filter will be implictly applied without them realizing. 这个方法将由所有子类实现,但过滤器将在没有实现的情况下被隐含地应用。

This approach works fine, my filter is executed. 这种方法工作正常,我的过滤器被执行。 But my Argument property is not being set to "dto" . 但是我的Argument属性没有设置为"dto"

This is my ValidateModelFilter 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 for the fact that my Argument property is always null , whether I apply the filter like this: [ValidateModelFilter] or this [ValidateModelFilter(Argument = "dto")] 一切正常,除了我的Argument属性始终为null ,我是否应用这样的过滤器: [ValidateModelFilter][ValidateModelFilter(Argument = "dto")]

Please, also note that I'm currently using an OWIN pipeline and Ninject for dependency injection. 请注意,我目前正在使用OWIN管道和Ninject进行依赖注入。

For future readers, I managed to solve the problem. 对于未来的读者,我设法解决了这个问题。

I was registering my filter in my application's configuration: 我在我的应用程序配置中注册了我的过滤器:

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

This makes it "Global" and is always executed, whether you have that filter annotation or not. 这使它成为“全局”始终执行,无论您是否具有该过滤器注释。

I commented that line and now my filter works fine. 我评论说这行,现在我的过滤器工作正常。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM