一个 ApiController paremeterless 构造函数错误,但另一个完美运行
One ApiController paremeterless constructor error, but another one works perfectly
我正在使用 .NET Framework 4.5.1 和 C# 开发 ASP.NET MVC 和 Web Api 2.2 应用程序。
当我执行 POST 到 http://myServer/api/Codes
:
时出现此错误
An error occurred when trying to create a controller of type
'CodesController'. Make sure that the controller has a parameterless
public constructor.
我安装了这个包:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr" version="3.5.0.2" targetFramework="net451" />
<package id="bootstrap" version="3.3.2" targetFramework="net451" />
<package id="DotNetZip" version="1.9.3" targetFramework="net451" />
<package id="EntityFramework" version="6.1.2" targetFramework="net451" />
<package id="jQuery" version="2.1.3" targetFramework="net451" />
<package id="jQuery.Validation" version="1.13.1" targetFramework="net451" />
<package id="log4net" version="2.0.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Client.es" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Core.es" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.WebHost.es" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.jQuery.Unobtrusive.Ajax" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" />
<package id="Modernizr" version="2.8.3" targetFramework="net451" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" />
<package id="Ninject" version="3.2.2.0" targetFramework="net451" />
<package id="Ninject.MVC5" version="3.2.1.0" targetFramework="net451" />
<package id="Ninject.Web.Common" version="3.2.3.0" targetFramework="net451" />
<package id="Ninject.Web.Common.WebHost" version="3.2.3.0" targetFramework="net451" />
<package id="Ninject.Web.WebApi" version="3.2.4.0" targetFramework="net451" />
<package id="WebActivatorEx" version="2.0.6" targetFramework="net451" />
<package id="WebGrease" version="1.6.0" targetFramework="net451" />
</packages>
但奇怪的是我有一个 Api 完美运行的控制器,以及两个不工作的 Api 控制器。
正在工作的 ApiController
构造函数:
public ExternalCodesController(
IGenericRepository<EXTERNAL_CODES> genericRepositoryECodes,
IGenericRepository<CHINA_CODES_HEADER> genericRepositoryHeader,
IGenericRepository<BATCHES> genericRepositoryBatches,
IGenericRepository<LINES> genericRepositoryLine,
IExternalCodesMapper externalCodesMapper,
IExceptionLogHelper exceptionLogger)
{
m_GenericRepositoryECodes = genericRepositoryECodes;
m_GenericRepositoryHeader = genericRepositoryHeader;
m_ExceptionLogger = exceptionLogger;
m_GenericRepositoryBatches = genericRepositoryBatches;
m_GenericRepositoryLine = genericRepositoryLine;
m_ExternalCodesMapper = externalCodesMapper;
}
不工作ApiController
构造函数:
public AggregationsController(
IGenericRepository<Data.AGGREGATIONS> repositoryAggs,
IGenericRepository<Data.AGGREGATION_CHILDS> repositoryAggChilds,
IGenericRepository<Data.CODES> repositoryCodes,
IAggregationsMapper aggregationMapper,
IAggregationChildsMapper aggChildsMapper,
IExceptionLogHelper exceptionLogger)
{
m_RepositoryAggs = repositoryAggs;
m_RepositoryAggChilds = repositoryAggChilds;
m_RepositoryCodes = repositoryCodes;
m_AggregationMapper = aggregationMapper;
m_AggChildsMapper = aggChildsMapper;
m_ExceptionLogger = exceptionLogger;
}
public CodesController(
IGenericRepository<CODES> repositoryCodes,
ICodesMapper codesMapper,
IExceptionLogHelper exceptionLogger)
{
m_RepositoryCodes = repositoryCodes;
m_CodesMapper = codesMapper;
m_ExceptionLogger = exceptionLogger;
}
对这个 Poltergeist 有什么想法吗?
如果您还需要什么,请告诉我。
您缺少必需的依赖项(未注册)并且 ninject 正在级联到不存在的无参数构造函数。
确定缺少哪个依赖项的一种方法是从构造函数中删除参数,直到您没有收到此错误。
我正在使用 .NET Framework 4.5.1 和 C# 开发 ASP.NET MVC 和 Web Api 2.2 应用程序。
当我执行 POST 到 http://myServer/api/Codes
:
An error occurred when trying to create a controller of type 'CodesController'. Make sure that the controller has a parameterless public constructor.
我安装了这个包:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr" version="3.5.0.2" targetFramework="net451" />
<package id="bootstrap" version="3.3.2" targetFramework="net451" />
<package id="DotNetZip" version="1.9.3" targetFramework="net451" />
<package id="EntityFramework" version="6.1.2" targetFramework="net451" />
<package id="jQuery" version="2.1.3" targetFramework="net451" />
<package id="jQuery.Validation" version="1.13.1" targetFramework="net451" />
<package id="log4net" version="2.0.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Client.es" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Core.es" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.WebHost.es" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.jQuery.Unobtrusive.Ajax" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" />
<package id="Modernizr" version="2.8.3" targetFramework="net451" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" />
<package id="Ninject" version="3.2.2.0" targetFramework="net451" />
<package id="Ninject.MVC5" version="3.2.1.0" targetFramework="net451" />
<package id="Ninject.Web.Common" version="3.2.3.0" targetFramework="net451" />
<package id="Ninject.Web.Common.WebHost" version="3.2.3.0" targetFramework="net451" />
<package id="Ninject.Web.WebApi" version="3.2.4.0" targetFramework="net451" />
<package id="WebActivatorEx" version="2.0.6" targetFramework="net451" />
<package id="WebGrease" version="1.6.0" targetFramework="net451" />
</packages>
但奇怪的是我有一个 Api 完美运行的控制器,以及两个不工作的 Api 控制器。
正在工作的 ApiController
构造函数:
public ExternalCodesController(
IGenericRepository<EXTERNAL_CODES> genericRepositoryECodes,
IGenericRepository<CHINA_CODES_HEADER> genericRepositoryHeader,
IGenericRepository<BATCHES> genericRepositoryBatches,
IGenericRepository<LINES> genericRepositoryLine,
IExternalCodesMapper externalCodesMapper,
IExceptionLogHelper exceptionLogger)
{
m_GenericRepositoryECodes = genericRepositoryECodes;
m_GenericRepositoryHeader = genericRepositoryHeader;
m_ExceptionLogger = exceptionLogger;
m_GenericRepositoryBatches = genericRepositoryBatches;
m_GenericRepositoryLine = genericRepositoryLine;
m_ExternalCodesMapper = externalCodesMapper;
}
不工作ApiController
构造函数:
public AggregationsController(
IGenericRepository<Data.AGGREGATIONS> repositoryAggs,
IGenericRepository<Data.AGGREGATION_CHILDS> repositoryAggChilds,
IGenericRepository<Data.CODES> repositoryCodes,
IAggregationsMapper aggregationMapper,
IAggregationChildsMapper aggChildsMapper,
IExceptionLogHelper exceptionLogger)
{
m_RepositoryAggs = repositoryAggs;
m_RepositoryAggChilds = repositoryAggChilds;
m_RepositoryCodes = repositoryCodes;
m_AggregationMapper = aggregationMapper;
m_AggChildsMapper = aggChildsMapper;
m_ExceptionLogger = exceptionLogger;
}
public CodesController(
IGenericRepository<CODES> repositoryCodes,
ICodesMapper codesMapper,
IExceptionLogHelper exceptionLogger)
{
m_RepositoryCodes = repositoryCodes;
m_CodesMapper = codesMapper;
m_ExceptionLogger = exceptionLogger;
}
对这个 Poltergeist 有什么想法吗?
如果您还需要什么,请告诉我。
您缺少必需的依赖项(未注册)并且 ninject 正在级联到不存在的无参数构造函数。
确定缺少哪个依赖项的一种方法是从构造函数中删除参数,直到您没有收到此错误。