簡體   English   中英

我可以將字典作為動作參數傳遞,但不能在復雜的 object 中傳遞

[英]I can pass dictionary as action parameter but can't pass it inside complex object

I want to pass IDictionary<string, List<string>> inside a DTO complex object into a ASP.NET 5 Api action using Postman.

首先,我嘗試通過正在工作的獨奏IDictionary<string, List<string>> 這是我的做法:

我使用這個請求正文:

{
    "Key1": [
        "Val1",
        "Val1"
    ],
    "Key2": [
        "Val1",
        "Val1"
    ]
}

當我像這樣將它傳遞給行動時,它運作良好:

        [HttpPut("{id}")]
        public IActionResult EditTags(int id, [FromBody] IDictionary<string, List<string>> tags)
        {
            return Ok();
        }

有用: 在此處輸入圖像描述

問題是我需要像這樣在復雜的 object 中包裝這個字典:

    public class DTO
    {
        public string SomeValue{ get; set; }

        IDictionary<string, List<string>> Tags{ get; set; }
     }

並將其作為操作的參數:

    [HttpPut("{id}")]
    public IActionResult EditTags(int id, [FromBody] DTO model)
    {
        //Do something with the model here
        return Ok();
    }

但是 model.tags 是 null。

我使用這個請求正文:

{
    "SomeValue": "Value",
    "Tags": {
        "Key1": [
            "Val1",
            "Val1"
        ],
        "Key2": [
            "Val1",
            "Val1"
        ]
    }
}

但是 model.Tags 是 null:

model.Tags 為空

我會嘗試首先將我的Tags屬性標記為公開:

public IDictionary<string, List<string>> Tags{ get; set; }

如果它仍然不起作用,我認為您需要在這種情況下使用JsonExtensionData屬性。 看看這個也可以了解更多細節。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM