嵌套列表JSON .NET [英] Nested list JSON .NET

查看:113
本文介绍了嵌套列表JSON .NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试接受对.NET Core Web API端点的POST请求,该端点接受以下模型:

I am currently trying to accept a POST request to a .NET Core Web API endpoint that accepts the following model:

public class OrderPost
{
    [Required]
    public string DeliveryStreet { get; set; }

    public string DeliveryBuilding { get; set; }

    [Required]
    public string DeliveryCity { get; set; }

    [Required]
    public string DeliveryProvince { get; set; }

    [Required]
    public string DeliveryPostalCode { get; set; }

    [Required]
    public string CardName { get; set; }

    [Required]
    public string CardNumber { get; set; }

    [Required]
    public string CardExpiry { get; set; }

    [Required]
    public long CardCvv { get; set; }

    [Required]
    public List<OrderItem> OrderItems { get; set; }
}

public class OrderItem
{
    public long Id { get; set; }
    public string Category { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public Uri ImageUrl { get; set; }
    public long Price { get; set; }
    public DateTimeOffset CreatedAt { get; set; } 
    public DateTimeOffset UpdatedAt { get; set; }
    public long Quantity { get; set; }
}

当我删除List<OrderItem>并在没有列表的情况下发布数据时,调用将完美运行.当我重新添加列表并尝试对数据进行POST时,我得到一个400 Bad Request错误.我有一种感觉,我只是缺少一些属性或列表中的某些内容.我发布的JSON有效,我仔细检查了一下.关于什么可能导致这种情况的任何想法?

When I remove the List<OrderItem> and post the data without the List the call works perfectly. As soon as I add the list back and try and POST the data I get a 400 Bad Request error. I have a feeling I am just missing some attribute or something on the List. The JSON I am posting is valid, I double checked that. Any ideas of what could be causing this?

这是我发布的JSON:

Here is the JSON I am POST'ing:

{
    "deliveryStreet": "Test street",
    "deliveryBuilding": "",
    "deliveryCity": "TEst",
    "deliveryProvince": "TEst",
    "deliveryPostalCode": "0852",
    "cardName": "Jane Jones",
    "cardNumber": "4711 1000 0000 0000",
    "cardExpiry": "05 / 20",
    "cardCvv": "123",
    "orderItems": [{
        "id": 1,
        "category": null,
        "name": "Test",
        "description": "Test test test",
        "imageUrl": "http://google.com/",
        "price": 625,
        "createdAt": "2019-02-21T13:25:56.709192",
        "updatedAt": "2019-02-21T13:25:56.709192",
        "quantity": 5
    }]
}

这是我要POST尝试的端点:

[HttpPost]
public IActionResult Post([FromBody]OrderPost data)
{
        //Get the credit card type off of the number
        var type = StringUtilities.GetCardTypeFromNumber(data.CardNumber);
        //Check if valid type was found
        if (type == "UNKNOWN")
            return BadRequest("Unknown credit card type");

        float total = 0;
        .................
}

推荐答案

对于Asp.Net Core Web api,模型验证错误会自动触发HTTP 400响应,请检查

For Asp.Net Core Web api, Model validation errors automatically trigger an HTTP 400 response, check Automatic HTTP 400 responses .

尝试使用以下代码进行测试:

Try to make a test with code below:

services.AddMvc()
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
    .ConfigureApiBehaviorOptions(options =>
    {
        options.SuppressModelStateInvalidFilter = true;
    });

这篇关于嵌套列表JSON .NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆