Model view controller 统一不登记
我在MVC应用程序中使用unity 我在Bootstapper.cs文件中有以下RegisterTypes方法:Model view controller 统一不登记,model-view-controller,asp.net-web-api,dependency-injection,unity-container,Model View Controller,Asp.net Web Api,Dependency Injection,Unity Container,我在MVC应用程序中使用unity 我在Bootstapper.cs文件中有以下RegisterTypes方法: public static void RegisterTypes(IUnityContainer container) { container.RegisterType<AccountController>(new InjectionConstructor()); container.RegisterType<IModelC
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<AccountController>(new InjectionConstructor());
container.RegisterType<IModelContext, ModelContext>();
container.RegisterType<IModelRepository, ModelRepository>();
}
我希望unity能够创建所需的ModelContext和ModelRepository对象。知道为什么不这样做吗?由于web api注册需要不同版本的system.web.http而导致的问题。我试图将web api添加到现有的mvc5应用程序中-这是个坏主意!我输入了一种自VB6 COM时代以来从未经历过的dll地狱形式。最后,我的解决方案是用一个WebAPI项目创建一个新的解决方案,然后重新安装mvc项目
public class APIScoresController : ApiController
{
private IModelRepository _repo;
public APIScoresController(IModelRepository repo)
{
_repo = repo;
}
public IEnumerable<Result> Get()
{
return _repo.GetResults();
}
}
public class ModelRepository : IModelRepository
{
ModelContext _ctx;
public ModelRepository(ModelContext ctx)
{
_ctx = ctx;
}
public IQueryable<DomainClasses.Result> GetResults()
{
return _ctx.Results;
}
}
An error occurred when trying to create a controller of type 'APIScoresController'. Make sure that the controller has a parameterless public constructor