简体   繁体   English

在 .NET Core 2.2 中嵌套资源 Web API

[英]Nesting resources in .NET Core 2.2 Web API

Sorry for being such a newbie on this subject but: I have a WEB API project and I am trying to use REST很抱歉在这个问题上是个新手,但是:我有一个 WEB API 项目,我正在尝试使用 REST

I have two classes Employee and Department.我有两个班级员工和部门。 I have a controller for each class对于每个 class,我都有一个 controller

On their own, I can view the classes我可以自己查看课程

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

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

and

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

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

Now... What I am trying to do is the following:现在......我想做的是以下几点:

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

I get localhost page cannot be found.我得到 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; }

}

Here is my Departments Controller这是我的部门 Controller

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

and here is my Employees controller这是我的员工 controller

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


}

In the Employee controller you can see that I am trying to get a second HTTPGET to get the department.在 Employee controller 中,您可以看到我正在尝试获取第二个 HTTPGET 来获取部门。 What am I missing here.我在这里想念什么。 I have never done this before and and have too many hours to continue to spin my wheels on it.我以前从未这样做过,并且有太多时间来继续旋转我的轮子。 Can anyone help me get my third type of request to work?谁能帮我完成我的第三类请求? (ie: (IE:

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

Thank you in advance先感谢您

The route is wrong.路线不对。 Please check:请检查:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM