【发布时间】:2017-04-27 19:31:23
【问题描述】:
据我所知,每个动作过滤器都应该继承 IActionFilter 和 FilterAttribute。 例如:
public class ActionSpeedProfilerAttribute : FilterAttribute, IActionFilter
{
public void OnActionExecuted(ActionExecutedContext filterContext)
{
throw new NotImplementedException();
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
throw new NotImplementedException();
}
}
http://www.dotnetcurry.com/aspnet-mvc/976/aspnet-mvc-custom-action-filter
但在 ASP.NET .Core 的 Microsoft 教程中,我们有没有 FilterAttribute 的示例:
public class SampleActionFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
// do something before the action executes
}
public void OnActionExecuted(ActionExecutedContext context)
{
// do something after the action executes
}
}
https://docs.microsoft.com/pl-pl/aspnet/core/mvc/controllers/filters
那么我们是否应该继承FilterAttribute?
【问题讨论】:
-
如果你想创建一个 ActionFilter 只需继承已经实现 IActionFilter-interface 的 ActionFilterAttribute。然后根据您的需要覆盖例如 OnActionExecuting-method。
-
@Esko 好的,但我也想知道使用 IActionFilter 时 FilterAttrubute 发生了什么。
标签: c# asp.net asp.net-mvc