0

So I am making a very simple web api in .Net with some data I found. One of the columns in this data is called Crop_Type. This column holds things like 'Corn', 'Wheat', 'Cofee'... It is pretty easy to make a get request function that queries the database for only one crop.

i.e. what I did:

[HttpGet("Crop_Type/{crop_type}")]
public async Task<ActionResult<List<CCImpactAgricultureItem>>> GetByCrop_Type(string crop_type) {...}

now let's say a user wanted to get all records where Crop_Type = 'Corn', Crop_Type = 'Wheat', and Crop_Type = 'Rice'. I guess the user could just make several API calls and join the returned data. However, I was wondering if there is a way to make another get function that can query for this case without having to make a function that explicitly states that it takes in 3 different variables. Like is there a more general way to make a get function that can take any number of different crop types?

2
  • How about just letting the user pass in a single or multiple type value? Crop_Type=Corn or Crop_Type=Corn,Wheat,Rice Commented Sep 26, 2024 at 17:55
  • There's no standard way to specify an array of values in a URI. Different frameworks use different techniques. In ASP.NET Core, you can use int[] id to parse id=1&id=2&id=3 Commented Sep 27, 2024 at 7:16

1 Answer 1

0

Am I wrong or you can follow this signature?:

public async Task<ActionResult<List<CCImpactAgricultureItem>>> GetByCrop_Type(string[] crop_types) {...}

taking string[] as parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

And what would the route in that HttpGet attribute be?
IMHO: GET /Crop_Type/Corn,Wheat,Soybean and it would need i.e. string.Split() logic in the method

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.