【问题标题】:C# How to send List as a param in url in mvc restapi?C#如何在mvc rest api的url中将List作为参数发送?
【发布时间】:2020-05-14 16:21:43
【问题描述】:

我一直在寻找这个问题的答案,但我并不总是得到我想要的。

这是我的 REST 控制器中的代码:

[Route("api/[controller]")]
[ApiController]
[Route("InsertOneProduct/{ean:long}/{name}/{producer}/{type}/{quantity:int}/{price:float}/{vat:int}")]
[HttpPost]
public void InsertOneProductWithType(long ean, string name, string producer, List<TypeOfProduct> type ,int quantity, float price, int vat)
{
     var product = new Products(ean, name, producer, type ,quantity, price, vat);

     mongoDB.InsertProducts(product);
}

我想要的是在 url 中发送类型列表,所以我尝试以这种方式以及更多方式这样做,但这不起作用

https://localhost:44385/api/Rest/InsertOneProduct/059013590036/TyskieGronie/KompaniaPiwowarska/type=Tyskie/100/1.99/23

https://localhost:44385/api/Rest/InsertOneProduct/059013590036/TyskieGronie/KompaniaPiwowarska/Tyskie/100/1.99/23

https://localhost:44385/api/Rest/InsertOneProduct/059013590036/TyskieGronie/KompaniaPiwowarska/type="Tyskie"/100/1.99/23

https://localhost:44385/api/Rest/InsertOneProduct/059013590036/TyskieGronie/KompaniaPiwowarska/"Tyskie"/100/1.99/23`  

产品代码和产品类型:

public class Products
{
    [BsonId]
    ObjectId objectId { get; set; }
    public long ean { get; set;}
    public string name { get; set; }
    public string producer { get; set; }

    public List<TypeOfProduct> type { get; set; }
    public float price { get; set; }
    public int vat { get; set; }
    public int quantity { get; set; }
}

public class TypeOfProduct
{
    string type;
}

有人可以解释如何正确地做到这一点吗?谢谢大家的回答

【问题讨论】:

  • 您似乎正在尝试在此路线上创建数据。您是否考虑过使用 POST 请求?您可以使用带有数组参数的 JSON(或其他格式)主体来传递多种类型。
  • 总的来说,我也质疑您选择使用这么多路径参数而不是更多查询参数的选择。类型是资源属性,而不是资源标识符。有关该主题的更多信息,请参阅here。由于路径参数通常不是复合数据,您可以尝试将其作为 URL 选项参数传入。

标签: c# list model-view-controller .net-core rest


【解决方案1】:

我不确定你能否完全按照你的要求去做。 但你可以这样做:

1) 从路由字符串中移除 {type}: "InsertOneProduct/{ean:long}/{name}/{producer}/{type}/{quantity:int}/{price:float}/{vat:int}"

2) 将属性 [FromQuery] 添加到 'type' 参数

3) 将 TypeOfProduct 类的字段 'type' 设为 public 并设为属性(添加 {get;set;})

结果代码:

[Route("api/[controller]")]
[ApiController]

  [Route("InsertOneProduct/{ean:long}/{name}/{producer}/{quantity:int}/{price:float}/{vat:int}")]
    [HttpPost]
    public void InsertOneProductWithType(long ean, string name, string producer, [FromQuery]List<TypeOfProduct> type ,int quantity, float price, int vat)
    {
        var product = new Products(ean, name, producer, type ,quantity, price, vat);

        mongoDB.InsertProducts(product);
    }

请求示例:

https://localhost:44385/api/Rest/InsertOneProduct/059013590036/TyskieGronie/KompaniaPiwowarska/"Tyskie"/100/1.99/23?type[0].type=asd&type[1].type=asdasdas

在 netcoreapp2.2 应用程序上测试

【讨论】:

  • 嗨!谢谢你的回答,我按照你说的做了,但是没有用,我发送请求时 List 类型的值为空。你能给我一些建议吗?
  • 哦,我的错。尝试将 TypeOfProduct 的字段“类型”作为属性(添加 {get;set;})
  • 我现在觉得有点傻,怎么能忘记get和set...再次感谢它有效!
猜你喜欢
  • 2017-03-19
  • 1970-01-01
  • 2020-05-26
  • 2014-07-14
  • 2018-01-23
  • 2018-08-17
  • 2022-06-16
  • 1970-01-01
  • 2016-08-03
相关资源
最近更新 更多