【发布时间】:2020-09-09 21:49:32
【问题描述】:
很抱歉在这个问题上是个新手,但是: 我有一个 WEB API 项目,我正在尝试使用 REST
我有两个班级 Employee 和 Department。 每个类都有一个控制器
我自己可以查看课程
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
找不到本地主机页面。
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 来获取部门。我在这里想念什么。我以前从未这样做过,并且有太多时间来继续旋转我的轮子。谁能帮我完成我的第三类请求? (即:
https://localhost:44309/api/employees/department/3)
提前谢谢你
【问题讨论】: