gpt4 book ai didi

ASP.NET Core 2.1 如何将变量传递给 TypeFilter

转载 作者:行者123 更新时间:2023-12-04 13:20:58 24 4
gpt4 key购买 nike

我创建了这个类型过滤器,它应该接受 2 个变量,以便将其发送到链接到过滤器的方法。但是,我无法附加我的 2 个变量以使其运行。

public class RolesFilterAttribute : TypeFilterAttribute
{
public RolesFilterAttribute() : base(typeof(RolesFilterAttributeImpl))
{

}

private 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();
}
}
}

这就是我声明过滤器的方式,它编译得很好。但是,我无法通过它传递所需的变量,即 SecretKey 和 Role。我的类型过滤器声明正确吗?

[TypeFilter(typeof(RolesFilterAttribute))]
public IActionResult About()
{
return View();
}

最佳答案

摘自官方documentation

[TypeFilter(typeof(AddHeaderAttribute),
Arguments = new object[] { "Author", "Steve Smith (@ardalis)" })]
public IActionResult Hi(string name)
{
return Content($"Hi {name}");
}

关于ASP.NET Core 2.1 如何将变量传递给 TypeFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52869075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com