【发布时间】:2018-12-20 11:33:58
【问题描述】:
我有我想调用的这个动作过滤器,我已经在 Startup.cs 中声明了它。但是,当我在班级上方调用它时,出现此错误:
LogUserNameFilter 不是属性类
我不确定我错过了什么。
public class LogUserNameFilter : IActionFilter
{
private readonly RequestDelegate next;
public LogUserNameFilter(RequestDelegate next)
{
this.next = next;
}
public void OnActionExecuted(ActionExecutedContext context)
{
throw new NotImplementedException();
}
public void OnActionExecuting(ActionExecutingContext context)
{
LogContext.PushProperty("UserName", context.HttpContext.User.Identity.Name);
}
}
Startup.cs
services.AddScoped<LogUserNameFilter>();
类声明
[LogUserNameFilter]
public class HomeController : Controller{
}
【问题讨论】:
-
您是否尝试将其实现为in this topic?即继承
ActionFilterAttribute而不是实现IActionFilter接口。 -
检查the documentation to see how to implement a filter attribute。话虽这么说,你在这里混合代码from your other question。过滤器中没有请求委托。
标签: asp.net-core asp.net-core-mvc