C# 找不到类型筛选器的合适构造函数
我有一个最近创建的typefilter,它位于一个单独的项目中C# 找不到类型筛选器的合适构造函数,c#,asp.net-core,asp.net-core-mvc,asp.net-core-2.1,C#,Asp.net Core,Asp.net Core Mvc,Asp.net Core 2.1,我有一个最近创建的typefilter,它位于一个单独的项目中 public class RolesFilterAttribute : TypeFilterAttribute { public RolesFilterAttribute() : base(typeof(RolesFilterAttributeImpl)) { } public class RolesFilterAttributeImpl : IActionFilter {
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>();
services.addScope();
services.addScope();
但是,当我启动应用程序并导航到“关于”页面时,我会遇到以下情况:
处理请求时发生未处理的异常。
InvalidOperationException:类型的合适构造函数
找不到“App.Link.Filters.RolesFilterAttribute”。确保
该类型是具体的,并且为的所有参数注册了服务
公共构造函数。
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(类型
instanceType,Type[]ArgumentType,out ConstructorInfo
matchingConstructor,out可为空[]参数映射)
我还遗漏了什么我没有申报的
[TypeFilter(typeof(RolesFilterAttribute), …]
这表示要创建的筛选器类型是rolesfilteratAttribute
。在类型筛选器中,还传递了两个参数“role”
和“abc1234”
。因此,当类型筛选器触发创建RolesFilterAttribute
时,它将查找接受这两个字符串的构造函数。但只有一个构造函数:
public RolesFilterAttribute()
: base(typeof(RolesFilterAttributeImpl))
{ }
因此,对于无参数构造函数,有两个参数。这就是为什么会出现错误
相反,您要做的是让[TypeFilter]
属性创建一个实际的过滤器。因此,您需要在此处输入rolesfilteratitributeimpl
:
[TypeFilter(typeof(RolesFilterAttributeImpl), Arguments = new object[] { "role", "abc1234" })]
在这一点上,您的rolesfilteratAttribute
也变得多余,因此您可以去掉它,只定义rolesfilteratitributeimpl
(我将其重命名为justRolesFilter
,因为它是一个过滤器,而不是一个属性或属性实现)
此外,由于您使用的是[TypeFilter]
,因此不需要向依赖项注入容器注册过滤器类型。您只需要注册类型的依赖项,因此在您的情况下只需注册ValidateRoleClient
因此,您的过滤器实现将如下所示:
public class RolesFilter : IActionFilter
{
private readonly ValidateRoleClient validateRoleClient;
private readonly string role;
private readonly string secretKey;
public RolesFilter(string role, string secretKey, ValidateRoleClient validateRoleClient)
{
this.validateRoleClient = validateRoleClient;
this.role = role;
this.secretKey = secretKey;
}
public void OnActionExecuted(ActionExecutedContext context)
{
// …
}
public void OnActionExecuting(ActionExecutingContext context)
{ }
}
[TypeFilter]
是顺便说一句。这只是允许使用属性指定过滤器的一种方法。您还可以创建自己的属性,该属性实际上只是一个过滤器工厂,负责在运行时创建过滤器实例。默认情况下,[TypeFilter]
就是这样做的
另见我的报告
这表示要创建的筛选器类型是rolesfilteratAttribute
。在类型筛选器中,还传递了两个参数“role”
和“abc1234”
。因此,当类型筛选器触发创建RolesFilterAttribute
时,它将查找接受这两个字符串的构造函数。但只有一个构造函数:
public RolesFilterAttribute()
: base(typeof(RolesFilterAttributeImpl))
{ }
因此,对于无参数构造函数,有两个参数。这就是为什么会出现错误
相反,您要做的是让[TypeFilter]
属性创建一个实际的过滤器。因此,您需要在此处输入rolesfilteratitributeimpl
:
[TypeFilter(typeof(RolesFilterAttributeImpl), Arguments = new object[] { "role", "abc1234" })]
在这一点上,您的rolesfilteratAttribute
也变得多余,因此您可以去掉它,只定义rolesfilteratitributeimpl
(我将其重命名为justRolesFilter
,因为它是一个过滤器,而不是一个属性或属性实现)
此外,由于您使用的是[TypeFilter]
,因此不需要向依赖项注入容器注册过滤器类型。您只需要注册类型的依赖项,因此在您的情况下只需注册ValidateRoleClient
因此,您的过滤器实现将如下所示:
public class RolesFilter : IActionFilter
{
private readonly ValidateRoleClient validateRoleClient;
private readonly string role;
private readonly string secretKey;
public RolesFilter(string role, string secretKey, ValidateRoleClient validateRoleClient)
{
this.validateRoleClient = validateRoleClient;
this.role = role;
this.secretKey = secretKey;
}
public void OnActionExecuted(ActionExecutedContext context)
{
// …
}
public void OnActionExecuting(ActionExecutingContext context)
{ }
}
[TypeFilter]
是顺便说一句。这只是允许使用属性指定过滤器的一种方法。您还可以创建自己的属性,该属性实际上只是一个过滤器工厂,负责在运行时创建过滤器实例。默认情况下,[TypeFilter]
就是这样做的
另见我的报告