从其他列表控制器返回类项目的列表? [Web API]

时间:2019-02-14 20:30:41

标签: c# .net

是否可以从其他类控制器返回“类”列表对象?我想创建一条返回ID所指定房屋中的公寓列表的路线

    [Route("api/House/{id}/Apartments")]
    public List<Apartment> Get()
    {
        return Apartments;
    }

我在HouseController中尝试了此操作,但它显示了一个错误-公寓在当前上下文中不存在。是否可以通过房屋管理员访问“公寓”列表,或者这样做是错误的方法?

public class House
{
    public string house_address { get; set; }
    public List<Apartment> apartments = new List<Apartment>();
    public Guid Id = Guid.NewGuid();
}
public class Apartment
{
    public string apartment_number { get; set; }
    public House House { get; set; }    
    public Guid Id = Guid.NewGuid();                                   
}

控制器:

  public class HousesController : ApiController
   { static List<House> Houses = new List<House>()
     public List<House> Get()
     { return Houses; }
   }
public class ApartmentsController : ApiController
   {static List<Apartment> Apartments = new List<Apartment>()
 }

0 个答案:

没有答案