【发布时间】: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