[HMACAuthentication()]
public class WeatherForecastController : ControllerBase
{
}
上面的代码是属性类,下面的代码是控制器。我想用参数调用属性
from scipy.ndimage import median_filter
values = [1,1,1,0,1,1,1,1,1,1,1,2,1,1,1,10,1,1,1,1,1,1,1,1,1,1,0,1]
print median_filter(values, 7, mode='mirror')
答案 0 :(得分:1)
考虑到参数必须是编译时常量,属性接受接口没有多大意义。
一种方法是您可以将接口注册为服务,并使用下面的代码获取它们,而无需构造函数注入。例如:
1。接口:
public interface IUserService
{
//..
}
public class UserService : IUserService
{
//..
}
2。启动时:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IUserService, UserService>();
}
3。自定义授权属性
public class HMACAuthenticationAttribute, IAsyncAuthorizationFilter
{
public HMACAuthenticationAttribute()
{
}
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
{
var user = context.HttpContext.RequestServices.GetRequiredService<IUserService>();
}
}
更新:
另一种方法是您也可以通过DI使用[ServiceFilter]
或[TypeFilter]
,请参阅
1。在启动时,注册HMACAuthenticationAttribute
:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<HMACAuthenticationAttribute>();
services.AddSingleton<IUserService, UserService>();
}
2。自定义授权属性
public class HMACAuthenticationAttribute, IAsyncAuthorizationFilter
{
public HMACAuthenticationAttribute(IUserService user)
{
}
}
3.Controller
[ServiceFilter(typeof(HMACAuthenticationAttribute))]
public class WeatherForecastController : ControllerBase
{
}