简体   繁体   English

.net core 3.0 构造函数参数问题

[英].net core 3.0 Constructor parameter problem

There is no argument given that corresponds to the required formal parameter 'userRoleService' of 'AuthorizeUserAttribute.AuthorizeUserAttribute(string, IUserRoleService, IModuleService, IUserService)'没有给出与“AuthorizeUserAttribute.AuthorizeUserAttribute(string, IUserRoleService, IModuleService, IUserService)”的所需形式参数“userRoleService”相对应的参数

AuthorizationController.cs授权控制器.cs

[AuthorizeUserAttribute("User.Edit")]
public ActionResult UserAuthorizationEdit()

AuthorizeUserAttribute.cs授权用户属性.cs

public string Action { get; set; }
private IUserRoleService _userRoleService;
private IModuleService _moduleService;
private IUserService _userService;

public AuthorizeUserAttribute(IUserRoleService userRoleService, IModuleService moduleService, IUserService userService)
{

    _userRoleService = userRoleService;
    _moduleService = moduleService;
    _userService = userService;
}

When I try to add constructor,controller side says write constructor as a parameter.当我尝试添加构造函数时,controller 方面说写构造函数作为参数。 How Can i change interface to a constructor如何将接口更改为构造函数

You need to use你需要使用

[TypeFileter(typeof(AuthorizeUser),Arguments = new object[] { "User.Edit" }))]
public ActionResult UserAuthorizationEdit(int userId, 
             RoleRegisterDto authorizationModel)

in order to dependency injection can inject your services.为了依赖注入可以注入你的服务。

If you want to uses interfaces via class constructor using DI,you need to pass the parameter with the same type from custom attribute on controller side.如果您想通过 class 构造函数使用 DI 来使用接口,您需要从 controller 端的自定义属性中传递相同类型的参数。

To avoid doing that, you could register your interfaces as services and get them using below code without constructor injection.For example:为避免这样做,您可以将接口注册为服务,并使用以下代码获取它们而无需构造函数注入。例如:

1.Interface 1.界面

public interface IUserRoleService
{
    List<string> GetValues();
}

public class UserRoleService : IUserRoleService
{
    private List<string> _privateList = new List<string>();

    public List<string> GetValues()
    {
        _privateList.Add("test");
        return _privateList;
    }
}

2.In startup: 2.启动时:

public void ConfigureServices(IServiceCollection services)
{
   services.AddSingleton<IUserRoleService, UserRoleService>();
}

3.Custom Authorization Attribute 3.自定义授权属性

public class AuthorizeUserAttribute:AuthorizeAttribute, IAsyncAuthorizationFilter
{
    public string Action { get; set; }

    public AuthorizeUserAttribute(string action)
    {
        Action = action;       
    }
    public async Task OnAuthorizationAsync(AuthorizationFilterContext authorizationFilterContext)
    {
        var x = authorizationFilterContext.HttpContext.RequestServices.GetRequiredService<IUserRoleService>();
        var y = x.GetValues();
    }
}

4.Action 4.动作

[AuthorizeUserAttribute("User.Edit")]
public ActionResult UserAuthorizationEdit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .net core 3.0,IClassFixture 问题,“未解析的构造函数参数:ITestOutputHelper 输出” - .net core 3.0, issue with IClassFixture, "unresolved constructor arguments: ITestOutputHelper output" 在.net核心中使用构造函数参数进行依赖注入 - Dependency injection with constructor parameter in .net core 在 .NET Core 上传递带有参数的构造函数作为泛型 - Pass constructor with parameter as generic on .NET Core .NET CORE 依赖注入将参数传递给构造函数 - .NET CORE dependency injection passing parameter to constructor Asp.Net Core 3.0 MVC Identity 添加角色问题 - Asp.Net Core 3.0 MVC Identity Adding Role Problem Dot Net Core 3.0 上的 Dapper 数据序列化问题 - Dapper data serialization problem on Dot Net Core 3.0 AspNetCoreRateLimit .NET Core 3.0 - 无法解析参数 IMemoryCache 缓存 - AspNetCoreRateLimit .NET Core 3.0 - Cannot resolve parameter IMemoryCache cache .net Core 3.0 GetMethod with types 参数不起作用 - .net Core 3.0 GetMethod with types parameter doesn't work 发布的参数在 .NET Core 3.0 API 中出现空白 - Posted parameter comes up blank in .NET Core 3.0 API .Net Core 3.0 Angular 应用程序发布请求参数始终为 NULL - .Net Core 3.0 Angular application post request parameter is always NULL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM