小编r31*_*r4r的帖子

.net Core 6 - 尝试激活时无法解析服务类型

在我的 ASP.NET Core 应用程序中,出现以下错误:

InvalidOperationException:尝试激活“mvc_net6.Controllers.StudentController”时无法解析类型“mvc_net6.Service.DB_Context”的服务。

StudentController StudentController 是通过 Scaffolds 创建到视图的,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using mvc_net6.Models;
using mvc_net6.Service;

namespace mvc_net6.Controllers
{
    public class StudentController : Controller
    {
        private readonly DB_Context _context;

        public StudentController(DB_Context context)
        {
            _context = context;
        }

        // GET: Student
        public async Task<IActionResult> Index()
        {
            return View(await _context.Students.ToListAsync());
        }

        // GET: Student/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var student …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc .net-core

13
推荐指数
3
解决办法
4万
查看次数

标签 统计

.net-core ×1

asp.net-mvc ×1

c# ×1