3

This thing is killing me. Basically what I have is the custom ActionFilter (Class which Inherits from ActionFilterAttribute and implements IActionFilter). it looks like this

public class ValidationFilterAttribute : ActionFilterAttribute, IActionFilter
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        // do some stuff here
    }
}

this is what FilterConfig looks like

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new ValidationFilterAttribute());
    }
}

But whenever I start the project there is an exception saying following

The given filter instance must implement one or more of the following filter interfaces: IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter.

But obviously the ValidationFilterAttribute implements one of those interfaces. Am I missing something very basic here? I can't figure out what is wrong.

3 Answers 3

8

Prefix your base class with System.Web.Mvc, I have a "hunch" you may be using System.Web.Http.Filters.ActionFilterAttribute instead.

So then you would have to override

OnActionExecuting(ActionExecutingContext filterContext)

And things will be OK.

And as you can see, ActionFilterAttribute implements IActionFilter already, so you don't need to specify it.

Sign up to request clarification or add additional context in comments.

Comments

1

As far as I know, you don't need the IActionFilter declaration. ActionFilterAttribute already implements IActionFilter

But I don't know if this is the cause of the error message. Could you try removing it?

Check this also: http://forums.asp.net/t/1835666.aspx

Comments

0
[Serializable] public class RedirectingAction : ActionFilterAttribute
{ 
    public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); 

}

after that use this attributes in your controller like below

[RedirectingAction] 
public class HomeController : BaseController { }

custom action filter example by www.jkittraining.com www.jkittraining.com/course/asp-net-mvc-training/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.