C# 在.NETCore2.2WebAPI中嵌套资源
很抱歉在这个问题上是个新手,但是: 我有一个WebAPI项目,我正在尝试使用REST 我有两个班:员工班和部门班。 我每个班都有一个控制器 我可以自己查看这些类C# 在.NETCore2.2WebAPI中嵌套资源,c#,rest,C#,Rest,很抱歉在这个问题上是个新手,但是: 我有一个WebAPI项目,我正在尝试使用REST 我有两个班:员工班和部门班。 我每个班都有一个控制器 我可以自己查看这些类 https://localhost:44309/api/employees/3 gives me the desired info of [{id:3,部门:空,部门id:1,名字:克里斯,姓氏:邓洛普,职务:软件开发人员,邮寄地址:3456卡尔加里西南第六街,阿尔伯塔省T1Y 6R5}] 及 [{id:3,姓名:HR,地址:阿尔伯
https://localhost:44309/api/employees/3 gives me the desired info of
[{id:3,部门:空,部门id:1,名字:克里斯,姓氏:邓洛普,职务:软件开发人员,邮寄地址:3456卡尔加里西南第六街,阿尔伯塔省T1Y 6R5}]
及
[{id:3,姓名:HR,地址:阿尔伯塔省卡尔加里西南第十街789号}]
现在。。。我想做的是:
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("department/{deptid}")]. // <-- HERE
public IEnumerable<Employee> GetByDepartmentId(int deptId)
{
GetAll();
return employees.Where(Employee => Employee.DepartmentID == deptId);
}
}
}
{
[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);
}
}
}
https://localhost:44309/api/employees/department/3)
{
[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);
}
}
}