【发布时间】:2018-10-18 11:04:03
【问题描述】:
我有这个最近创建的类型过滤器,它位于一个单独的项目中。
public class RolesFilterAttribute : TypeFilterAttribute
{
public RolesFilterAttribute() : base(typeof(RolesFilterAttributeImpl))
{
}
public class RolesFilterAttributeImpl : IActionFilter
{
private readonly ValidateRoleClient validateRoleClient;
private string Role;
private string SecretKey;
public RolesFilterAttributeImpl(string Role, string SecretKey, ValidateRoleClient validateRoleClient)
{
this.validateRoleClient = validateRoleClient;
this.Role = Role;
this.SecretKey = SecretKey;
}
public void OnActionExecuted(ActionExecutedContext context)
{
if (context.HttpContext.Request.Cookies["Token"] != null || context.HttpContext.Request.Cookies["RefreshToken"] != null)
{
TokenViewModel tvm = new TokenViewModel
{
Token = context.HttpContext.Request.Cookies["Token"],
RefreshToken = context.HttpContext.Request.Cookies["RefreshToken"]
};
ValidateRoleViewModel vrvm = new ValidateRoleViewModel
{
Role = Role,
SecretKey = SecretKey,
Token = tvm
};
validateRoleClient.ValidateRole(vrvm);
}
}
public void OnActionExecuting(ActionExecutingContext context)
{
throw new NotImplementedException();
}
}
}
这就是我在方法上方声明它的方式:
[TypeFilter(typeof(RolesFilterAttribute), Arguments = new object[] { "role", "abc1234" })]
public IActionResult About()
{
return View();
}
我相信我已经在 Startup.cs 中声明了我需要的内容
services.AddScoped<ValidateRoleClient>();
services.AddScoped<RolesFilterAttribute>();
但是,当我启动应用程序并导航到 about 页面时,我遇到了这样的情况:
处理请求时发生未处理的异常。 InvalidOperationException:适合类型的构造函数 找不到“App.Link.Filters.RolesFilterAttribute”。确保 类型是具体的,并且为所有参数注册了服务 公共构造函数。 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(类型 instanceType, Type[] argumentTypes, out ConstructorInfo 匹配构造器,出 Nullable[] parameterMap)
还有什么我没有声明的遗漏?
【问题讨论】:
标签: c# asp.net-core asp.net-core-mvc asp.net-core-2.1