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

<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>

  

后台:

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

    }

}  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 ASP.NET Core WebAPI 项目,你可以通过使用 Model Binding 功能来解析 JSON 数据并将其转换为 User 类对象。以下是一个简单的示例代码: 首先,确保你的 User 类具有与 JSON 数据字段对应的属性。例如: ```csharp public class User { public string Name { get; set; } public int Age { get; set; } // 其他属性... } ``` 然后,在你的控制器的相应方法,使用 `[FromBody]` 特性将 JSON 数据绑定到 User 类对象上。例如: ```csharp [HttpPost] public IActionResult CreateUser([FromBody] User user) { // 这里的 user 参数将包含从请求解析的 JSON 数据 // 执行其他操作,如保存到数据库 return Ok(); } ``` 在上述示例,`CreateUser` 方法使用 `[FromBody]` 特性将请求的 JSON 数据绑定到名为 `user` 的 User 类对象上。你可以根据需要在该方法执行相应的操作,比如将 user 对象保存到数据库。 当发送 POST 请求时,确保请求的 Content-Type 设置为 `application/json`,并提供符合 User 类结构的 JSON 数据。例如: ```http POST /api/users HTTP/1.1 Content-Type: application/json { "Name": "John", "Age": 25 } ``` 这样,ASP.NET Core WebAPI 就会自动将请求的 JSON 数据解析为 User 对象,并将其传递给 `CreateUser` 方法的 user 参数。 注意:为了使 Model Binding 可以正常工作,你需要在 `Startup.cs` 文件添加适当的配置。例如,在 `ConfigureServices` 方法添加以下代码: ```csharp services.AddControllers() .AddJsonOptions(options => { options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; options.JsonSerializerOptions.PropertyNamingPolicy = null; }); ``` 这样,你就可以在 ASP.NET Core WebAPI 项目解析 JSON 数据 User 类,并进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值