WebApi 中FromUri参数自动解析成实体的要求

条件一:类属性名称必须和参数名称相同(不分大小写)

条件二:API参数必须以[FromUri]来修饰(数组也需要添加,否则参数传递不了)

条件三:属性类型为“类”的,如果使用类名(导航属性在本类的名称,可以不是类的原名).属性名或者 类参数名[属性] 的形式,例如Page.PageIndex 或者Page[]PageIndex]

条件四:属性类型为“数组,集合”时,如果带上下标,如类名[0].属性名的形式,例如OrderList[0].OrderId

条件五:属性为类时,要求这个类必须有空的构造方法

条件六:属性的set块,必须是public修饰的(不要用public 字段,否则参数传递不了)

 

测试链接:

http://localhost:57879/api/test/OrderBy?nMebID=87225&abc[Parts][0][ps]=111111111part&abc[Parts][1][pS]=222222222222part&abc[LastKey]=Last Key&abc[ins][]=1&abc[ins][]=2&abc[ins][]=3&abc[P][pS]=P-pS&part1[pS]=p1-pS&part2[pS]=p2-pS&zis[]=1&zis[]=2&zis[]=3

测试前端:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

  

后台:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

using System.Text;

using System.Web.Http;

using Newtonsoft.Json;

 

namespace WebApplication1.Controllers

{

    public class TestController : ApiController

    {

        [HttpGet]

        public string OrderBy(int nMebID, [FromUri] PartsQuery ABC, [FromUri] Part part1, [FromUri] Part part2, int[] zis)

        {

            QueryDiscountCouponsCondition ee = new QueryDiscountCouponsCondition();

            return JsonConvert.SerializeObject(ABC);

        }

    }

}

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

namespace WebApplication1

{

    public class PartsQuery

    {

        public int[] ins { getset; }

        public Part[] Parts { getset; }

        public string LastKey { getset; }

        public int zhengshu { getset; }

        public bool bl { getset; }

 

        public Part P { getset; }

    }

 

    public class Part

    {

        public int[] pIns { getset; }

        public string pS { getset; }

        public int pInt { getset; }

    }

}  

你可能感兴趣的:(ASP.NET,webapi)