我该如何解决这个webapi项目错误?

时间:2020-01-16 15:43:24

标签: c# asp.net-core webapi

我正在为学校制作一个WebApi核心项目,在我决定启动该项目进行测试之前,一切似乎都还不错。当我启动它时,出现此错误https://imgbbb.com/images/2020/01/16/error.png

这是我的项目组成的所有内容:

project tree

这是控制器代码:

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using webapi3.Models;
using webapi3.Models.Repository;
using RouteAttribute = Microsoft.AspNetCore.Components.RouteAttribute;

namespace webapi3.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        private readonly IDataRepository<Employee> _dataRepository;

        public EmployeeController(IDataRepository<Employee> dataRepository)
        {
            _dataRepository = dataRepository;
        }

        // GET: api/Employee
        [HttpGet]
        public IActionResult Get()
        {
            IEnumerable<Employee> employees = _dataRepository.GetAll();
            return Ok(employees);
        }

        // GET: api/Employee/5
        [HttpGet("{id}", Name = "Get")]
        public IActionResult Get(long id)
        {
            Employee employee = _dataRepository.Get(id);

            if (employee == null)
            {
                return NotFound("The Employee record couldn't be found.");
            }

            return Ok(employee);
        }

        // POST: api/Employee
        [HttpPost]
        public IActionResult Post([FromBody] Employee employee)
        {
            if (employee == null)
            {
                return BadRequest("Employee is null.");
            }

            _dataRepository.Add(employee);
            return CreatedAtRoute(
                  "Get",
                  new { Id = employee.EmployeeId },
                  employee);
        }

        // PUT: api/Employee/5
        [HttpPut("{id}")]
        public IActionResult Put(long id, [FromBody] Employee employee)
        {
            if (employee == null)
            {
                return BadRequest("Employee is null.");
            }

            Employee employeeToUpdate = _dataRepository.Get(id);
            if (employeeToUpdate == null)
            {
                return NotFound("The Employee record couldn't be found.");
            }

            _dataRepository.Update(employeeToUpdate, employee);
            return NoContent();
        }

        // DELETE: api/Employee/5
        [HttpDelete("{id}")]
        public IActionResult Delete(long id)
        {
            Employee employee = _dataRepository.Get(id);
            if (employee == null)
            {
                return NotFound("The Employee record couldn't be found.");
            }

            _dataRepository.Delete(employee);
            return NoContent();
        }
    }
}

如果有人帮助我,我会非常感激!预先感谢!

1 个答案:

答案 0 :(得分:0)

尝试在[Route(“ api / [controller]”))中将[controller]更改为“ Employee”。从Microsoft 1

检查此示例

用控制器的名称替换[controller],按照惯例,该名称是控制器类名称减去“ Controller”后缀。对于此示例,控制器类名称为TodoItemsController,因此控制器名称为“ TodoItems”。 ASP.NET Core路由不区分大小写。

另一个选择是,检查您的Startup.cs文件(将其发布在此处,以便我们检查是否一切正常)。并检查此示例以说明.NET Attribute Routing

中的属性路由
    public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}