一个ApiController paremeterless构造函数错误,但另一个完美的工作

时间:2015-03-26 13:14:25

标签: c# asp.net asp.net-web-api ninject

我正在使用.NET Framework 4.5.1和C#开发ASP.NET MVC和Web Api 2.2应用程序。

当我对http://myServer/api/Codes发布POST时出现此错误:

  

尝试创建类型的控制器时发生错误   ' CodesController&#39 ;.确保控制器具有无参数   公共建设者。

我安装了这个软件包:

<?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>

但奇怪的是我有一个完美的ApiController和两个不起作用的ApiControllers。

工作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;
}

对这个吵闹鬼的任何想法?

如果您还需要别的东西,请告诉我。

1 个答案:

答案 0 :(得分:1)

您缺少必需的依赖项(未注册),ninject正在级联到不存在的无参数构造函数。

确定缺少哪个依赖项的一种方法是从构造函数中删除参数,直到您没有收到此错误。