首页 > 解决方案 > 在 .NET Core 2.2 Web API 中嵌套资源

问题描述

很抱歉在这个主题上是个新手,但是:我有一个 WEB API 项目,我正在尝试使用 REST

我有两个班级员工和部门。我为每个班级都有一个控制器

我可以自己查看课程

https://localhost:44309/api/employees/3 gives me the desired info of 

[{"id":3,"department":null,"departmentID":1,"firstName":"Chris","lastName":"Dunlop","jobTitle":"软件开发人员","mailingAddress":" 3456 6th Street SW Calgary Alberta T1Y 6R5"}]

https://localhost:44309/api/departments/3 gives me the desired info of 

[{"id":3,"name":"HR","address":"789 10th Street SW Calgary Alberta"}]

现在......我想做的是以下几点:

https://localhost:44309/api/employees/department/3

我得到 localhost 页面找不到。

public class Employee
    {
        #region Properties

        public int ID { get; set; }

        [ForeignKey("DepartmentID")]
        public Department Department { get; set; }

        public int DepartmentID { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string JobTitle { get; set; }

        public string MailingAddress { get; set; }

        #endregion Properties
    }

public class Department
{
    public int ID { get; set; }

    public string Name { get; set; }

    public string Address { get; set; }

}

这是我的部门控制器

{
    [Route("api/Departments")]
    [ApiController]
    public class DepartmentsController : ControllerBase
    {
        private List<Department> departments = new List<Department>();

        // GET: api/Departments
        [HttpGet]
        public IEnumerable<Department> GetAll()
        {
            departments.Add(new Department { ID = 1, Name = "Application Development", Address = "123 4th Street NW Calgary Alberta" });
            departments.Add(new Department { ID = 2, Name = "Management", Address = "456 7th Street NE Calgary Alberta" });
            departments.Add(new Department { ID = 3, Name = "HR", Address = "789 10th Street SW Calgary Alberta" });

            return departments;            
        }

        // GET api/Departments/5
        [HttpGet("{id}")]
        public IEnumerable<Department> Get(int id)
        {
            GetAll();
            return departments.Where(departments => departments.ID == id);
        }
    }
}

这是我的员工控制器

{
    [Route("api/Employees")]
    [ApiController]

    public class EmployeesController : ControllerBase
    {

        private List<Employee> employees = new List<Employee>();

        // GET: api/Employees
        [HttpGet]
        public IEnumerable<Employee> GetAll()
        {
            employees.Add(new Employee { ID = 1, DepartmentID = 1, FirstName = "Chris", LastName = "Dunlop", JobTitle = "Software Developer", MailingAddress = "1234 4th Avenue NW Calgary Alberta T1Y 6R5" });
            employees.Add(new Employee { ID = 2, DepartmentID = 1, FirstName = "Chris", LastName = "Dunlop", JobTitle = "Software Developer", MailingAddress = "2345 5th Street NE Calgary Alberta T1Y 6R5" });
            employees.Add(new Employee { ID = 3, DepartmentID = 1, FirstName = "Chris", LastName = "Dunlop", JobTitle = "Software Developer", MailingAddress = "3456 6th Street SW Calgary Alberta T1Y 6R5" });

            employees.Add(new Employee { ID = 4, DepartmentID = 2, FirstName = "Vince", LastName = "O'Gorman", JobTitle = "Development Supervisor", MailingAddress = "1201 49th Street NE Calgary Alberta T1Y 6R5" });
            employees.Add(new Employee { ID = 5, DepartmentID = 3, FirstName = "Tracey", LastName = "Jarvis", JobTitle = "HR Manager", MailingAddress = "1301 69th Street SW Calgary Alberta T1Y 6R5" });

            return employees;
        }

        // GET api/Employees/1
        [HttpGet("{id}")]
        public IEnumerable<Employee> Get(int id)
        {
            GetAll();
            return employees.Where(Employee => Employee.ID == id);
        }

        // GET api/Employees/Department/1
        [HttpGet("int/{deptid}")]
        public IEnumerable<Employee> Get2(int deptId)
        {
            GetAll();
            return employees.Where(Employee => Employee.DepartmentID == deptId);            
        }
    }


}

在 Employee 控制器中,您可以看到我正在尝试获取第二个 HTTPGET 来获取部门。我在这里想念什么。我以前从未这样做过,并且有太多时间来继续旋转我的轮子。谁能帮我完成我的第三类请求?(IE:

https://localhost:44309/api/employees/department/3)

先感谢您

标签: c#rest

解决方案


路线不对。请检查:

{
    [Route("api/Employees")]
    [ApiController]

    public class EmployeesController : ControllerBase
    {

        private List<Employee> employees = new List<Employee>();

        // GET: api/Employees
        [HttpGet]
        public IEnumerable<Employee> GetAll()
        {
            employees.Add(new Employee { ID = 1, DepartmentID = 1, FirstName = "Chris", LastName = "Dunlop", JobTitle = "Software Developer", MailingAddress = "1234 4th Avenue NW Calgary Alberta T1Y 6R5" });
            employees.Add(new Employee { ID = 2, DepartmentID = 1, FirstName = "Chris", LastName = "Dunlop", JobTitle = "Software Developer", MailingAddress = "2345 5th Street NE Calgary Alberta T1Y 6R5" });
            employees.Add(new Employee { ID = 3, DepartmentID = 1, FirstName = "Chris", LastName = "Dunlop", JobTitle = "Software Developer", MailingAddress = "3456 6th Street SW Calgary Alberta T1Y 6R5" });

            employees.Add(new Employee { ID = 4, DepartmentID = 2, FirstName = "Vince", LastName = "O'Gorman", JobTitle = "Development Supervisor", MailingAddress = "1201 49th Street NE Calgary Alberta T1Y 6R5" });
            employees.Add(new Employee { ID = 5, DepartmentID = 3, FirstName = "Tracey", LastName = "Jarvis", JobTitle = "HR Manager", MailingAddress = "1301 69th Street SW Calgary Alberta T1Y 6R5" });

            return employees;
        }

        // GET api/Employees/1
        [HttpGet("{id}")]
        public IEnumerable<Employee> Get(int id)
        {
            GetAll();
            return employees.Where(Employee => Employee.ID == id);
        }

        // GET api/Employees/Department/1
        [HttpGet("department/{deptid}")]. // <-- HERE
        public IEnumerable<Employee> GetByDepartmentId(int deptId)
        {
            GetAll();
            return employees.Where(Employee => Employee.DepartmentID == deptId);            
        }
    }
}

推荐阅读