错误 occurred.Type 'my api name controller' 在 System.Linq.Expressions.Expression 处没有默认值 constructorSystem.ArgumentException
An error has occurred.Type 'my api name controller' does not have a default constructorSystem.ArgumentException at System.Linq.Expressions.Expression
我正在尝试使用 angular
创建一个 mvc 项目。所以我创建了一个 api controller
,如您在此处所见:
namespace MvcApplication3.Controllers
{
public class TopicsController : ApiController
{
private IMessageBoardRepository _repo;
public TopicsController(IMessageBoardRepository repo)
{
_repo = repo;
}
public IEnumerable<Topic> Get(bool includeReplies = false)
{
IQueryable<Topic> results;
if (includeReplies == true)
{
results = _repo.GetTopicsIncludingReplies();
}
else
{
results = _repo.GetTopics();
}
var topics = results.OrderByDescending(t => t.Created)
.Take(25)
.ToList();
return topics;
}
// I didn't show this, but this is common
public HttpResponseMessage Get(int id, bool includeReplies = false)
{
IQueryable<Topic> results;
if (includeReplies == true)
{
results = _repo.GetTopicsIncludingReplies();
}
else
{
results = _repo.GetTopics();
}
var topic = results.Where(t => t.Id == id).FirstOrDefault();
if (topic != null) return Request.CreateResponse(HttpStatusCode.OK, topic);
return Request.CreateResponse(HttpStatusCode.NotFound);
}
}
}
我正在使用 ninject 3.2.1.0
将我的界面绑定到它的存储库。
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<MessageBoardContext>().To<MessageBoardContext>().InRequestScope();
kernel.Bind<IMessageBoardRepository>().To<MessageBoardRepository>().InRequestScope();
}
但是当我用这个 api 调用这个 url 时:http://localhost:53005/api/v1/topics
我收到这个错误:
An error has occurred.Type 'MvcApplication3.Controllers.TopicsController' does not have a default constructorSystem.ArgumentException at System.Linq.Expressions.Expression.New(Type type)
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
最后我应该安装 WebApiContrib.IoC.Ninject
nuget 并将此解析器添加到 ninjectwebcommen
文件中,如您在此处所见:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver =
new NinjectResolver(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
我正在尝试使用 angular
创建一个 mvc 项目。所以我创建了一个 api controller
,如您在此处所见:
namespace MvcApplication3.Controllers
{
public class TopicsController : ApiController
{
private IMessageBoardRepository _repo;
public TopicsController(IMessageBoardRepository repo)
{
_repo = repo;
}
public IEnumerable<Topic> Get(bool includeReplies = false)
{
IQueryable<Topic> results;
if (includeReplies == true)
{
results = _repo.GetTopicsIncludingReplies();
}
else
{
results = _repo.GetTopics();
}
var topics = results.OrderByDescending(t => t.Created)
.Take(25)
.ToList();
return topics;
}
// I didn't show this, but this is common
public HttpResponseMessage Get(int id, bool includeReplies = false)
{
IQueryable<Topic> results;
if (includeReplies == true)
{
results = _repo.GetTopicsIncludingReplies();
}
else
{
results = _repo.GetTopics();
}
var topic = results.Where(t => t.Id == id).FirstOrDefault();
if (topic != null) return Request.CreateResponse(HttpStatusCode.OK, topic);
return Request.CreateResponse(HttpStatusCode.NotFound);
}
}
}
我正在使用 ninject 3.2.1.0
将我的界面绑定到它的存储库。
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<MessageBoardContext>().To<MessageBoardContext>().InRequestScope();
kernel.Bind<IMessageBoardRepository>().To<MessageBoardRepository>().InRequestScope();
}
但是当我用这个 api 调用这个 url 时:http://localhost:53005/api/v1/topics
我收到这个错误:
An error has occurred.Type 'MvcApplication3.Controllers.TopicsController' does not have a default constructorSystem.ArgumentException at System.Linq.Expressions.Expression.New(Type type)
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
最后我应该安装 WebApiContrib.IoC.Ninject
nuget 并将此解析器添加到 ninjectwebcommen
文件中,如您在此处所见:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver =
new NinjectResolver(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}