簡體   English   中英

運行時出現 InvalidOperationException ASP.NET Core MVC Web App

[英]InvalidOperationException while running ASP.NET Core MVC Web App

我剛開始學習 ASP.NET Core MVC C#。我嘗試從 Udemy 課程中學習,但他們使用的是舊版本,相信它是 2.0。 但我正在嘗試構建一個 note taking web 應用程序。 我在處理請求時不斷收到未處理的異常。 后跟 InvalidOperationException:嘗試激活“Notely_Application.Controllers.HomeController”時無法解析類型“Notely_Application.Repository.INoteRepository”的服務。

這是兩者的源代碼:

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

您需要 INoteRepository 的實現,您有嗎? 並且你必須在 Startup.cs 的 ConfigureServices() 方法中添加依賴注入映射,類似於類似的一行

services.AddTransient<INoteRepository, ClassThatImplementsINoteRepository>();

首先我建議你使用

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

然后准確閱讀ex中的錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM