Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 namespace Chegg.Infrastructure { public interface IStaffRepository { Staff Get(int id); } }

Chegg.Infrastructure 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; } } }

Chegg.Infrastructure> StaffRepository.cs

namespace Chegg.Infrastructure { public class StaffRepository : IStaffRepository { public Staff Get(int id) { return AllStaff().First(x => x.Id == id); } private IEnumerable AllStaff() { return new List new Staff() { Id = 182, FullName = "Ed Sheeran", Email = "edsheeran@singer.com", JobTitle = "Great Singer" }, new Staff() { Id = 100, FullName = "Adele", Email = "adele@singer.com", Likes = new [] {"sing","cakes"} }, }; } } 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; }

Chegg.UI>Controllers > HomeController.cs

The AllStaff() at this row underlined with red. 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); } }

Chegg.UI > Models > Staff.cs

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; } } }

Chegg.UI > Models > StaffViewModel.cs

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; } } }

index.cshtml

@{ ViewData["Title"] = "Home Page"; }

Staff List

Select to load

program.cs

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

More Books

Students also viewed these Databases questions