0

I am looking for answer for this question for a lot of time, but I don't always get what I want.

This is the code in my REST controller:

[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);
}

This what I want is to send List of types in url, so i try to do this this way and many more but this doesn't work

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`  

Code of products and type of products:

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;
}

Can somebody explain how to do it properly? Thanks for all answers

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Krutek
  • 3
  • 2
  • It looks like you're trying to create data on this route. Have you considered using a POST request instead? You could use a JSON (or other format) body with an array parameter to pass the multiple types through. – David Bodow May 15 '20 at 02:47
  • In general, I'm also questioning your choice to use so many path parameters instead of more query parameters. Types are resource properties, not resource identifiers. See [here](https://stackoverflow.com/questions/30967822/when-do-i-use-path-params-vs-query-params-in-a-restful-api) for more on that topic. Since path parameters are usually not compound data, you could try passing that in as URL option parameters instead. – David Bodow May 15 '20 at 02:49

1 Answers1

0

I'm not sure you can do exactly what you asking. But you can do something like this:

1) Remove {type} from route string: "InsertOneProduct/{ean:long}/{name}/{producer}/{type}/{quantity:int}/{price:float}/{vat:int}"

2) Add attribute [FromQuery] to 'type' parameter

3) Make field 'type' of class TypeOfProduct public and make it as property (add {get;set;})

result code:

[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);
    }

Request example:

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

Tested on netcoreapp2.2 application

  • Hi! Thanks you for answer, i did what you say but it didn't work, values of List type is empty when i send the request. Can you give me some advice? – Krutek May 14 '20 at 17:43
  • Oh, my bad. Try to make field 'type' of TypeOfProduct as property (add {get;set;}) – Ruslan Kamev May 14 '20 at 17:54
  • I feel now a little stupid, how a can forget about get and set... Thank you again it works! – Krutek May 14 '20 at 17:59