简体   繁体   English

尝试创建“ XXXXController”类型的控制器时发生错误。 确保控制器具有无参数的公共构造函数

[英]An error occurred when trying to create a controller of type 'XXXXController'. Make sure that the controller has a parameterless public constructor

I have created a asp.net web api project and implemented the below HTTP GET method in AccountController and the related service method & repository method in AccountService & AccountRepository respectively. 我创建了一个asp.net Web API项目,并分别在AccountController实现了以下HTTP GET方法,并在AccountServiceAccountRepository分别实现了相关的服务方法和存储库方法。

// WEB API 
public class AccountController : ApiController
{
    private readonly IAccountService _accountService;

    public AccountController(IAccountService accountService)
    {
        _accountService = accountService;
    }

    [HttpGet, ActionName("UserProfile")]
    public JsonResult<decimal> GetUserSalary(int userID)
    {
        var account = _accountService.GetUserSalary(userID);
        if (account != null)
        {
            return Json(account.Salary);
        }
        return Json(0);
    }
}

Service / Business Layer 服务/业务层

public interface IAccountService
{
    decimal GetUserSalary(int userId);
}

public class AccountService : IAccountService
{
    readonly IAccountRepository _accountRepository = new AccountRepository();

    public decimal GetUserSalary(int userId)
    {
        return _accountRepository.GetUserSalary(userId);
    }
}

Repository / Data Access Layer 储存库/数据访问层

public interface IAccountRepository
{
    decimal GetUserSalary(int userId);
}

public class AccountRepository : IAccountRepository
{
    public decimal GetUserSalary(int userId)
    {
        using (var db = new AccountEntities())
        {
            var account = (from b in db.UserAccounts where b.UserID == userId select b).FirstOrDefault();
            if (account != null)
            {
                return account.Salary;
            }
        }
        return 0;
    }
}

UnityConfig UnityConfig

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();
        container.RegisterType<IAccountService, AccountService>();
        container.RegisterType<IAccountRepository, AccountRepository>();
        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
    }
}

But when I invoke the API method GetUserSalary() I get an error saying 但是,当我调用API方法GetUserSalary()我收到一条错误消息:

An error occurred when trying to create a controller of type 'AccountController'. 尝试创建“ AccountController”类型的控制器时发生错误。 Make sure that the controller has a parameterless public constructor. 确保控制器具有无参数的公共构造函数。

Check that you did not forget to register Unity IoC container itself: 确保您没有忘记注册Unity IoC容器本身:

  • if you use ASP.NET Framework it could be - Global.asax or Startap.cs (Owin) via UnityConfig.RegisterComponents() method. 如果使用ASP.NET Framework,则可能是-通过UnityConfig.RegisterComponents()方法的Global.asax或Startap.cs(Owin)。
  • if you use ASP.NET Core then in the Startup.cs file (I was unable to find official guides for its configuting) 如果您使用ASP.NET Core,则在Startup.cs文件中(我无法找到有关其配置的官方指南)

Your current constructor has parameters (or args if you prefer). 您当前的构造函数具有参数(如果您愿意,可以使用args)。

see: 看到:

public AccountController(IAccountService accountService)
{
    _accountService = accountService;
}

All you need to do is add a "Parameter-less Constructor" into the controller as well. 您所需要做的就是向控制器中添加一个“无参数构造函数”。

public AccountController()
{
}

Parameter-less constructors are usually above the ones that have params, though as far as I am aware this is only due to standards not any actual effect(s) it may cause. 无参数构造函数通常在具有参数的构造函数之上,尽管据我所知,这仅是由于标准,而不是可能引起的任何实际影响。

There is also an already existing issue/question similar to this I will link below that may provide further details. 还有一个与此类似的已经存在的问题/问题,我将在下面链接,可能会提供更多详细信息。

Make sure that the controller has a parameterless public constructor error 确保控制器具有无参数的公共构造函数错误

暂无
暂无

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

相关问题 尝试创建X类型的控制器时发生错误。请确保该控制器具有无参数构造函数 - An error occurred when trying to create a controller of type X. Make sure that the controller has a parameterless constructor unity DI错误-确保控制器具有无参数的公共构造函数 - unity DI error - Make sure that the controller has a parameterless public constructor 带Breeze控制器的LightInject-确保控制器具有无参数的公共构造函数 - LightInject with Breeze Controller - Make sure that the controller has a parameterless public constructor 简单注入器 - 确保控制器在生产时具有无参数的公共构造函数 - Simple Injector - Make sure that the controller has a parameterless public constructor on production Autofac - 确保控制器具有无参数的公共构造函数 - Autofac - Make sure that the controller has a parameterless public constructor ASP.NET Identity Manager错误:尝试创建“MetaController”类型的控制器时出错(无参数公共构造函数) - ASP.NET Identity Manager Error: error when trying to create a controller of type 'MetaController' (no parameterless public constructor) Autofac Web API 错误确保控制器具有无参数的公共构造函数 - Autofac Web API error Make sure that the controller has a parameterless public constructor 类在控制器中没有无参数的构造函数异常 - Class has no parameterless constructor exception in controller 我没有使用依赖注入,但我收到“尝试创建类型控制器时出错” - I'm not using dependency injection, but I'm getting "An error occurred when trying to create a controller of type" 运行时控制器中的无参数构造函数错误 - Parameterless Constructor error in my controller at run time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM