Reputation: 1735
Just wondering, googling around how to use filters in asp mvc 4. I have found some people defines them like this:
public class CustomFilter : ActionFilterAttribute
And some like this:
public class CustomFilter : ActionFilterAttribute, IActionFilter
ActionFilterAttribute already has all operations to override, why should i implement the interface as well?
Also for example in the next code, at the end the filter is called again, why is this?
public class
CustomActionFilter : ActionFilterAttribute, IActionFilter
{
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{
// TODO: Add your acction filter's tasks here
// Log Action Filter Call
MusicStoreEntities storeDB = new MusicStoreEntities();
ActionLog log = new ActionLog()
{
Controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
Action = filterContext.ActionDescriptor.ActionName + " (Logged By: Custom
Action Filter)",
IP = filterContext.HttpContext.Request.UserHostAddress,
DateTime = filterContext.HttpContext.Timestamp
};
storeDB.ActionLogs.Add(log);
storeDB.SaveChanges();
this.OnActionExecuting(filterContext);
}
}
Upvotes: 3
Views: 1376
Reputation: 32490
If you check the MSDN definition of ActionFilterAttribute you will see that it is an abstract class that inherits interfaces IActionFilter AND IResultFilter.
In effect inheriting from ActionFilterAttribute,
public class CustomFilter : ActionFilterAttribute
is equivalent to inheriting class FilterAttribute and interfaces IActionFilter and IResultFilter
public class CustomFilter : FilterAttribute, IActionFilter, IResultFilter
and is no different than
public class CustomFilter : ActionFilterAttribute, IActionFilter, IResultFilter
So inheriting from ActionFilterAttribute is no different than inheriting from ActionFilterAttribute and IActionFilter.
Upvotes: 2