只是想知道,谷歌搜索如何在 asp mvc 4 中使用过滤器。我发现有些人这样定义它们:
public class CustomFilter : ActionFilterAttribute
还有一些是这样的:
public class CustomFilter : ActionFilterAttribute, IActionFilter
ActionFilterAttribute 已经有所有要覆盖的操作,为什么还要实现接口呢?
同样例如在下一个代码中,最后再次调用过滤器,这是为什么呢?
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);
}
}