Azure Function - Read Query String Parameters in HttpTrigger

This article mainly focus on read query string parameter in Azure Function with Http Trigger.

There are multiple ways to do it.

Here dotnet 5 and dotnet-isolated used for Azure Functions.

Solution - 1

If we consider following url, then Id and name is query string parameters.

http://localhost:7071/api/ReadFromQueryParameters?id=1&name=test
[Function("ReadFromQueryParameters")]
public static async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
         FunctionContext executionContext)
        {
            var queryParameters = HttpUtility.ParseQueryString(req.Url.Query);           
            var logger = executionContext.GetLogger("ReadFromQueryParameters");
            logger.LogInformation("C# HTTP trigger function processed a request.");
            var response = req.CreateResponse(HttpStatusCode.OK);
            await response.WriteAsJsonAsync(ConvertToDictionary(queryParameters));
            return response;
        }

private static StringDictionary ConvertToDictionary(NameValueCollection valueCollection)
        {
            var dictionary = new StringDictionary();
            foreach (var key in valueCollection.AllKeys)
            {
                if (key != null)
                {
                    dictionary.Add(key.ToLowerInvariant(), valueCollection[key]);
                }
            }
            return dictionary ;
        }

Result is something like following.

[{"Key":"id","Value":"1"},{"Key":"name","Value":"test"}]

Solution - 2

Now solution - 1 is good when you have limited parameter and some of the case when there are too many parameter and it is good if we can deserialize directly to object then it is good. It is somewhat like binding works in asp.net mvc.

For this we are going to use FunctionContext.

For this let's first create class.

 public class InputValue
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

Now function will look like following.

[Function("ReadToObject")]
        public static async Task<HttpResponseData> ReadToObject([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
            FunctionContext executionContext)
        {
            var jsonData = executionContext.BindingContext.BindingData["Query"];
            var inputData = Newtonsoft.Json.JsonConvert.DeserializeObject<InputValue>(jsonData.ToString());
            var logger = executionContext.GetLogger("ReadFromQueryParameters");
            logger.LogInformation("C# HTTP trigger function processed a request.");
            var response = req.CreateResponse(HttpStatusCode.OK);
            await response.WriteAsJsonAsync(inputData);
            return response;
        }

Now in above following two lines are important.

var jsonData = executionContext.BindingContext.BindingData["Query"];
var inputData = Newtonsoft.Json.JsonConvert.DeserializeObject<InputValue>(jsonData.ToString());

Now in first line it will read value from BindingData and Query parameter contains all query parameters as json. In second line it deserialize to object. This will give strongly type object so it is more useful.

Result is something like following.

{"Id":1,"Name":"test"}

Note: Just to remember that in Azure function it is also possible to provide custom route and route parameter. Above two method will not help in that.