最佳答案
我创建了一个。net核心MVC应用程序,并使用依赖注入和存储库模式将存储库注入到我的控制器。然而,我得到一个错误:
无法解析类型为“WebApplication1.Data”的服务。当试图激活“WebApplication1.Controllers.BlogController”。
存储库:
public interface IBloggerRepository { ... }
public class BloggerRepository : IBloggerRepository { ... }
控制器:
public class BlogController : Controller
{
private readonly IBloggerRepository _repository;
public BlogController(BloggerRepository repository)
{
_repository = repository;
}
public IActionResult Index() { ... }
}
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddScoped<IBloggerRepository, BloggerRepository>();
}
我不知道我哪里做错了。什么好主意吗?