skip to Main Content

I’m new to learning ASP.NET Core MVC C#. I tried learning from a Udemy course, but they were using an old version believe it was 2.0. But I’m trying to build a note taking web application. I keep receiving an Unhandled exception occurred while processing the request. Followed by
InvalidOperationException: Unable to resolve service for type ‘Notely_Application.Repository.INoteRepository’ while attempting to activate ‘Notely_Application.Controllers.HomeController’.

Here is the source code for both:

Notely_Application.Repository.INoteRepository

using System;
using System.Collections.Generic;
using Notely_Application.Models;

namespace Notely_Application.Repository
{
    public interface INoteRepository
    {
        NotesModel FindNoteById(Guid id);
        IEnumerable<NotesModel> GetAllNotes();
        void SaveNote(NotesModel notesModel);
        void DeleteNote(NotesModel notesModel);
    }
}

Notely_Application.Controllers.HomeController

using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Notely_Application.Models;
using Notely_Application.Repository;

namespace Notely_Application.Controllers
{
    public class HomeController : Controller
    {
        private readonly INoteRepository _noteRepository;

        public HomeController(INoteRepository noteRepository)
        {
            _noteRepository = noteRepository;
        }

        public IActionResult Index()
        {
            var notes = _noteRepository.GetAllNotes().Where(n => n.IsDeleted == false);
            return View(notes);
        }

        public IActionResult NoteDetail(Guid id)
        {
            var note = _noteRepository.FindNoteById(id);
            return View(note);
        }

        [HttpGet]
        public IActionResult NoteEditor(Guid id = default)
        {
            if(id != Guid.Empty)
            {
                var note = _noteRepository.FindNoteById(id);

                return View(note);
            }
            return View();
        }

        [HttpPost]
        public IActionResult NoteEditor(NotesModel notesModel)
        {
            if (ModelState.IsValid)
            {
                var date = DateTime.Now;
                if (notesModel != null && notesModel.Id == Guid.Empty)
                {
                    notesModel.Id = Guid.NewGuid();
                    notesModel.CreatedDate = date;
                    notesModel.LastModified = date;

                    _noteRepository.SaveNote(notesModel);
                }
                else
                {
                    var note = _noteRepository.FindNoteById(notesModel.Id);
                    note.LastModified = date;
                    note.Subject = notesModel.Subject;
                    note.Detail = notesModel.Detail;
                }
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

        public IActionResult DeleteNote(Guid id)
        {
            var note = _noteRepository.FindNoteById(id);

            note.IsDeleted = true;

            return RedirectToAction("Index");
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

2

Answers


  1. First of all I suggest you to use

    try
    {
        // my code here
    }
    catch (Exception ex)
    {
        // my error here -> ex       
    }
    

    and then read exactly what error is inside ex

    Login or Signup to reply.
  2. You need an implementation of INoteRepository, do you have that? And you have to add the dependency injection mapping in Startup.cs in the ConfigureServices() method, a line like similar

    services.AddTransient<INoteRepository, ClassThatImplementsINoteRepository>();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search