C# WebAPI和mvc4类型';发生错误;。控制器.APICategoryController';没有默认构造函数

C# WebAPI和mvc4类型';发生错误;。控制器.APICategoryController';没有默认构造函数,c#,asp.net-mvc-4,asp.net-web-api,C#,Asp.net Mvc 4,Asp.net Web Api,我有这个错误作为标题提到,是什么原因导致这个人??这是我的密码 我只想显示SQL数据库中的文件,但它确实有错误。有什么帮助吗?谢谢 namespace a.Models public interface ICatRepository { IEnumerable<Category> GetAll(); Category Get(int id); Category Add(Category item); void Remove(int id);

我有这个错误作为标题提到,是什么原因导致这个人??这是我的密码 我只想显示SQL数据库中的文件,但它确实有错误。有什么帮助吗?谢谢

    namespace a.Models

public interface ICatRepository
{
    IEnumerable<Category> GetAll();
    Category Get(int id);
    Category Add(Category item);
    void Remove(int id);
    bool Update(Category item);
}
名称空间a.模型
公共接口存储库
{
IEnumerable GetAll();
类别Get(int-id);
类别添加(类别项);
无效删除(int-id);
bool更新(类别项目);
}
另一个存储库

namespace a.Models

public class CatRepository : ICatRepository
{

    private istellarEntities db = new istellarEntities();

    public CatRepository()
    {
    }

    public IEnumerable<Category> GetAll()
    {
        return db.Categories;
    }

    public Category Get(int id)
    {
        return db.Categories.Find(id);
    }

    public Category Add(Category category)
    {
        db.Categories.Add(category);
        db.SaveChanges();
        return category;
    }

    public void Remove(int id)
    {
        Category category = db.Categories.Find(id);
        db.Categories.Remove(category);
        db.SaveChanges();
    }

    public bool Update(Category category)
    {
        db.Entry(category).State = EntityState.Modified;
        db.SaveChanges();
        return true;
    }
}
名称空间a.模型
公共类CatRepository:CatRepository
{
私有istellarEntities db=新的istellarEntities();
公共CatRepository()
{
}
公共IEnumerable GetAll()
{
返回db.Categories;
}
公共类别获取(int-id)
{
返回db.Categories.Find(id);
}
公共类别添加(类别)
{
db.Categories.Add(类别);
db.SaveChanges();
退货类别;
}
公共无效删除(int id)
{
Category Category=db.Categories.Find(id);
db.Categories.Remove(类别);
db.SaveChanges();
}
公共布尔更新(类别)
{
db.Entry(category).State=EntityState.Modified;
db.SaveChanges();
返回true;
}
}
控制器

namespace a.Controllers

public class APICategoryController : ApiController
{
   // static readonly ICatRepository repository = new CatRepository();

 private readonly ICatRepository repository;

 public APICategoryController(ICatRepository repository)
    {
        if (repository == null)
        {
            throw new ArgumentNullException("repository");
        }
        this.repository = repository;
    }
    public IEnumerable<Category> GetAllCategories()
{
    return repository.GetAll();
}
名称空间a.控制器
公共类APICategoryController:ApiController
{
//静态只读存储库repository=new CatRepository();
专用只读存储库;
公共ApicCategoryController(ICAT存储库)
{
if(存储库==null)
{
抛出新的ArgumentNullException(“存储库”);
}
this.repository=存储库;
}
公共IEnumerable GetAllCategories()
{
返回repository.GetAll();
}
最后是我的班级档案

namespace a.Models

using System;
using System.Collections.Generic;

public partial class Category
{
    public Category()
    {
        this.IQuestions = new HashSet<IQuestion>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public virtual ICollection<IQuestion> IQuestions { get; set; }
}
名称空间a.模型
使用制度;
使用System.Collections.Generic;
公共部分类
{
公共类别()
{
this.IQuestions=new HashSet();
}
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection IQuestions{get;set;}
}

您需要添加默认构造函数。您应该将其添加到APICategoryController(这是默认构造函数)


这里有一条更有用的错误消息-APICategoryController没有默认构造函数,即:无参数构造函数

您需要为代码使用某种依赖项注入器,以了解如何为存储库实例化具体类,或者提供默认构造函数

例如:


我已经添加了,但似乎还有另一个错误,'ObjectContent'1'类型未能序列化内容类型的响应主体'application/xml;charset=utf-8',这实际上是一个单独的问题。可能与延迟加载有关(跨实体导航属性的递归序列化)我补充说,但是对象引用没有被设置为对象的实例。我知道你在问一个具体的问题……但是你确实应该考虑存储库模式。这里是我见过的最好的例子。
  public APICategoryController()
  {
  }
     //a default constructor instantiating a concrete type. Simple, but no good for testing etc.
     public APICategoryController()
     {
         ICatRepository repository = new ConcreteRepository;
         this.repository = repository;
     }