码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-11-27 12:40:15      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:ring   har   last   构造方法   bsp   newton   tco   abc   上下   

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

条件二: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

测试前端:

<script type="text/javascript" charset="utf-8">
    $(function () { 
         
        $.ajax({
            type: "get",
            url: "http://localhost:57879/api/test/OrderBy",
            dataType: "text",
            data: {
                "nMebID":87225,
                "abc": { Parts: [{ ps: "111111111part" }, { pS: "222222222222part" }], LastKey: "Last Key", ins: [1, 2, 3], P: { pS: "P-pS"} },
                "part1": { pS: "p1-pS" },
                "part2": { pS: "p2-pS" },
                "zis":[1,2,3]
            },
            success: function (data) {
                alert(data);
            },
            error: function (x, y, z) {
              //  alert("报错无语");
            }
        });
    });
</script>

  

后台:

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);
        }
    }
}

  

namespace WebApplication1
{ 
    public class PartsQuery
    {
        public int[] ins { get; set; }
        public Part[] Parts { get; set; }
        public string LastKey { get; set; }
        public int zhengshu { get; set; }
        public bool bl { get; set; }

        public Part P { get; set; }
    }

    public class Part
    {
        public int[] pIns { get; set; }
        public string pS { get; set; }
        public int pInt { get; set; }
    } 
}  

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

标签:ring   har   last   构造方法   bsp   newton   tco   abc   上下   

原文地址:http://www.cnblogs.com/lonelyxmas/p/7903488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!