小编Mah*_*san的帖子

在我的 POST 请求操作中,JsonProperty 映射不起作用

我有一个如下所示的模型和控制器

public class OrderModel
{
    [JsonProperty("order_id")]        
    public string ID { get; set; }

    [JsonProperty("delivery_shift")]
    public string DeliveryShift { get; set; }
    public string InvoiceType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
[Route("api/[controller]")]
[ApiController]
public class OrderController : ControllerBase
{
    [HttpPost]
    public IActionResult Index([FromBody] OrderModel order) {

        return new JsonResult(new
        {
            order.ID,
            order.DeliveryShift,
            order.InvoiceType
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我在正文中提交带有此 json 的 post 请求order_id并且delivery_shift不与模型映射时。

{"order_id": "OrderID", "delivery_shift": "evening", "InvoiceType": "DUE"}
Run Code Online (Sandbox Code Playgroud)

但是当我以这种格式提交 json 时它可以工作,但我需要以前的格式才能工作

{"ID": "OrderID", "DeliveryShift": "evening", "InvoiceType": "DUE"} …
Run Code Online (Sandbox Code Playgroud)

c# asp.net json asp.net-core-3.1

6
推荐指数
0
解决办法
3080
查看次数

标签 统计

asp.net ×1

asp.net-core-3.1 ×1

c# ×1

json ×1