Question
InvalidOperationException: Unable to resolve service for type '...................................Repository' while attempting to activate '...............................................HomeController'. Folder: Chegg.UI>Controllers > HomeController.cs The AllStaff() at this row underlined with red.
InvalidOperationException: Unable to resolve service for type '...................................Repository' while attempting to activate '...............................................HomeController'.
Folder: Chegg.UI>Controllers > HomeController.cs
The AllStaff() at this row underlined with red. var allStaff = _staffRepository.AllStaff();
:( I always run into few bugs. Something Im missing out am sure. I got confused.
I can not find where the mistake. Please help me with a detailed code and description. Somewhere probably I misunderstood what you tried to help. I connected with ProjectReference the Infrastructure folder at the UI \ Dependencies by right click..
Please be kind and check these out:
--------------------------------------------------------------
Folder:
Chegg.Infrastructure
> Dependences
> IStaffRepository.cs
> Staff.cs
> StaffRepository.cs
Chegg.UI
> Connected Services
> Dependencies
> Properties
> wwwroot
>Controllers
> HomeController.cs
> Models
> Staff.cs
> StaffViewModel.cs
> Views
> Home
> Index.cshtml
etc.
> appsettings.json
> Program.cs
=============================================================================
My code:
Chegg.Infrastructure Chegg.Infrastructure Chegg.Infrastructure> StaffRepository.cs Chegg.UI>Controllers > HomeController.cs The AllStaff() at this row underlined with red. Chegg.UI > Models > Staff.cs Chegg.UI > Models > StaffViewModel.cs index.cshtml Select to load program.cs namespace Chegg.Infrastructure { public interface IStaffRepository { Staff Get(int id); } }
namespace Chegg.InterviewDec2023.Infrastructure { public class Staff { public int Id { get; set; } public string FullName { get; set; } public string Email { get; set; } public string JobTitle { get; set; } public string[] Likes { get; set; } } }
namespace Chegg.Infrastructure { public class StaffRepository : IStaffRepository { public Staff Get(int id) { return AllStaff().First(x => x.Id == id); } private IEnumerable
var allStaff = _staffRepository.AllStaff();
namespace Chegg.UI.Controllers; public class HomeController : Controller { private readonly IStaffRepository _staffRepository; public HomeController(IStaffRepository staffRepository) { _staffRepository = staffRepository; } public IActionResult Index() { return View(); } [HttpGet] public IActionResult GetAllStaff() { var allStaff = _staffRepository.AllStaff(); var staffViewModels = allStaff.Select(staff => new StaffViewModel { Id = staff.Id, FullName = staff.FullName, Email = staff.Email, JobTitle = staff.JobTitle, Likes = staff.Likes }); return Json(staffViewModels); } [HttpGet] public IActionResult GetDetails(int id) { var staff = _staffRepository.Get(id); var staffViewModel = new StaffViewModel { Id = staff.Id, FullName = staff.FullName, Email = staff.Email, JobTitle = staff.JobTitle, Likes = staff.Likes }; return Json(staffViewModel); } }
namespace Chegg.UI.Models { public class Staff { public int Id { get; set; } public string FullName { get; set; } public string Email { get; set; } public string JobTitle { get; set; } public string[] Likes { get; set; } } }
namespace Chegg.UI.Models { public class StaffViewModel { public int Id { get; set; } public string FullName { get; set; } public string Email { get; set; } public string JobTitle { get; set; } public string[] Likes { get; set; } } }
@{ ViewData["Title"] = "Home Page"; }
Staff List
var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); var app = builder.Build(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run();
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started