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

时间:2022-05-30 08:34:09

条件一:类属性名称必需和参数名称不异(不分巨细写)

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

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

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

条件五:属性为类时,要求这个类必需有空的结构要领

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

测试链接:

: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参数自动解析成实体的要求