Azure-docs: Broken code

Created on 30 Jul 2018  Â·  5Comments  Â·  Source: MicrosoftDocs/azure-docs

The following code does not compile:

string name = req.GetQueryNameValuePairs()
    .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
    .Value;

GetQueryNameValuePairs is no longer available. It looks like it may have been replaced by GetQueryParameterDictionary. So it would be something like:

string name = req.GetQueryParameterDictionary()
    .FirstOrDefault(p => string.Equals(p.Key, "name", StringComparison.CurrentCultureIgnoreCase))
    .Value;

Note: I used string.Equals instead of string.Compare, because I think it's clearer.

Also, for what it's worth, the HttpTrigger template uses this instead:

string name = req.Query["name"];

Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

assigned-to-author doc-bug functionsvc triaged

Most helpful comment

It would be easier for the customers if we could maintain consistency on the sample code provided in the doc and the template. We can follow what has been provided in the Http Trigger template (when user creates a Azure Function project in Visual Studio):

            // parse query parameter
            string name = req.Query["name"];

           // Get request body
            string requestBody = new StreamReader(req.Body).ReadToEnd();
            dynamic data = JsonConvert.DeserializeObject(requestBody);

          // Set name to query string or body data
            name = name ?? data?.name;

            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");

All 5 comments

@MisinformedDNA Thank you for the valuable feedback,we are investigating the issue.

It would be easier for the customers if we could maintain consistency on the sample code provided in the doc and the template. We can follow what has been provided in the Http Trigger template (when user creates a Azure Function project in Visual Studio):

            // parse query parameter
            string name = req.Query["name"];

           // Get request body
            string requestBody = new StreamReader(req.Body).ReadToEnd();
            dynamic data = JsonConvert.DeserializeObject(requestBody);

          // Set name to query string or body data
            name = name ?? data?.name;

            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");

@Karishma-Tiwari-MSFT Please assign to @ggailey777

@MisinformedDNA and @Karishma-Tiwari-MSFT unfortunately, this is not a simple issue to address. The code in that example is actually correct and from the C# HTTP template for the Azure Functions v1 runtime. Unfortunately, there have been changes to the C# template to support the Functions v2 runtime, which is why you are seeing different code and the v1 code doesn't compile.

I am investigating how to best fix this, but we may end up having to put in both examples to avoid this confusion until v2 goes GA.

@MisinformedDNA sorry for the inconvenience, but we are behind in getting examples in this article updated for v2 (.NET Core APIs). This is being tracked internally in this work item: #1380883. Since this issue is being tracked, we can #please-close per the feedback SLA. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

behnam89 picture behnam89  Â·  3Comments

spottedmahn picture spottedmahn  Â·  3Comments

Favna picture Favna  Â·  3Comments

Ponant picture Ponant  Â·  3Comments

bdcoder2 picture bdcoder2  Â·  3Comments