question

skdev avatar image
0 Votes"
skdev asked skdev commented

Azure function v3 - bind comma separated query string value to array/list in .net

Hi,
I have a use case to bind query string to a model in Azure function V3 and in that, say, one of the query string will have array values. It is a .net core function app.

For eg: <url>?item=laptop&places=Bangalore&places=Chennai&places=Mumbai

This should be bound to a model like:

public class Items{

public string item { get; set; }
public List<string> places { get; set; }

}

Tried using FromQuery which doesn't seems to be supported in Azure function.

Could you please suggest?

azure-functions
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MayankBargali-MSFT avatar image
0 Votes"
MayankBargali-MSFT answered skdev commented

@sanal-kumar-p-2758 Sharing previous discussion in stackoverflow. In the same thread there is another answer if you have predefine format and does not change.

For your example you can use below code to get all places ?item=laptop&places=Bangalore&places=Chennai&places=Mumbai and similarly get other fields from query string and then bind it to your class.

  string[] places = req.Query["places"].ToArray();
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I wanted to bind it directly to the model and hence don't want to use the code you provided.
I am looking for a solution more like the one given in the stackoverflow link you provided.
But looks like it doesn't work with IFunctionHostBuilder.

0 Votes 0 ·

Hi @MayankBargali-MSFT
Also, I am looking for a solution which can also bind the below url as well to the same object mentioned in the question.

?item=laptop&places=Bangalore,Chennai,Mumbai

public class Items{

public string item { get; set; }
public List<string> places { get; set; }

}

0 Votes 0 ·