【问题标题】:passing list of objects to get method in web API传递对象列表以获取 Web API 中的方法
【发布时间】:2021-03-23 14:44:15
【问题描述】:

如何将对象列表传递给 web api get 方法。 我不是想发布这个列表,只是想对这个数据做一些函数并返回一个值。 我尝试了以下代码

        [Route("Save_SALE")]
        [HttpGet]
        [AllowAnonymous]
        public IHttpActionResult Save_SALE(List<SL_table> SL_table)
{
                  return Ok(SL_table);
}

它似乎返回空列表,任何人都可以告诉我出了什么问题

【问题讨论】:

    标签: c# api web asp.net-web-api


    【解决方案1】:

    1)你不能用“Httpget”传递数据 2)json数据必须与你的类字段相同

    示例 json 数据:

    [
        {
            "date": "2020-11-01T10:47:25",
            "checkNo": "2020300400474",
            "siteNo": "2001",
            "siteName": "Koroglu",
            "taxId": "0122345789",
            "companyName": "AA Company MMC",
            "amount": 30,
            "description": "description main",
            "description2": "description additional"
            
        },
        {
            "date": "2020-11-06T11:17:38",
            "checkNo": "2020300400479",
            "siteNo": "2002",
            "siteName": "20 yanvar",
            "taxId": "0122345789",
            "companyName": "AB Company MMC",
            "amount": 240,
            "description": "description main",
            "description2": "description additional"
    
        }
    ]
    

    和示例 Web API 方法

      [HttpPost]
        
                public async Task<IActionResult> AddInvoice(List<Invoice> invoices)
                {
                   return Ok(invoices)
                }
    

    如果您想使用 HttpGet 传递数据,请使用查询参数

    【讨论】:

    • 谢谢,查询参数是什么意思,对不起,我对 web api 很陌生
    • 搜索 Http Query 参数
    猜你喜欢
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 2019-06-14
    • 2019-10-21
    • 2023-03-03
    • 2016-08-10
    相关资源
    最近更新 更多