复杂类型

时间:2018-06-18 16:55:10

标签: asp.net-web-api

型号:

[DataContract]
public class Employee
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [DataMember(Name ="id")]
    public int Id{ get; set; }

    [DataMember(Name = "fullName")]
    public string FullName { get; set; }
}

[DataContract]
public class Department
{
    public Department()
    {
        this.Employees = new List<Employee>();
    }

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [DataMember(Name = "id")]
    public int Id { get; set; }

    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "employees")]
    public List<Employee> Employees { get; set; }
}

控制器

public HttpResponseMessage Get([FromUri]Department model)
{
   if (ModelState.IsValid)
   {

   }
   return new HttpResponseMessage(HttpStatusCode.OK);

}

网址http://localhost:2070/home/get/?id=1&name=IT&Employees=1,John

我正在尝试调用上面的URL,而Model不会读取Employees。 Model读取其他属性,如int,double,string,decimal。 任何人都可以帮助我通过List to through Url的正确格式。

另外,我不想用modelbinders和我控制器中的参数来装饰我的每个类。

Tech:WebApi,.Net3.5

1 个答案:

答案 0 :(得分:0)

使用FromUri和list / array时需要指定要绑定的列表和属性的索引

以这种方式试试

http://localhost:2070/home/get/?id=1&name=IT&Employees[0].Id=1&Employees[0].Name=John