[英]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方法,并在AccountService
和AccountRepository
分别实现了相关的服务方法和存储库方法。
// 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容器本身:
UnityConfig.RegisterComponents()
method. 如果使用ASP.NET Framework,则可能是-通过UnityConfig.RegisterComponents()
方法的Global.asax或Startap.cs(Owin)。 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.