0

I have an HTTP Trigger Azure Function which is currently in 1.x. The code is as below:

using System.Net;
using System.Threading.Tasks;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");

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

    // Get request body
    dynamic data = await req.Content.ReadAsAsync<object>();

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

    return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}

While trying to upgrade it to 2.x, I am getting an issue with GetQueryNameValuePairs

I am getting error - 'HttpRequestMessage' does not contain a definition for 'GetQueryNameValuePairs'

Is there no support for this method in 2.0? How can this be accomplished in .net standard?

Silly John
  • 1,584
  • 2
  • 16
  • 34

3 Answers3

0

Function runtime 1.x is on Full .Net Framework, while 2.x runs on .NET Core env and our function code targets at .NET Standard.

For this class HttpRequestMessage, it doesn't have GetQueryNameValuePairs method in .NET Standard assembly.

Migrating from 1.x to 2.x usually needs work of code modification. Since it's just a template, I suggest you delete it and recreate a Http Trigger in 2.x runtime. You may see a different template work with .NET Standard.

Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
  • Do we have an equivalent for that in .net standard. I could not find anything. Or should we manually parse the query string? – Silly John Sep 29 '18 at 11:57
  • 2
    @SillyJohn If you have created a http trigger template in 2.x, you can see it uses `HttpRequest req` and `req.Query["name"]`. Is this the equivalent you need? – Jerry Liu Sep 29 '18 at 12:56
0

Here is the sample code that looks up query string parameters in Functions V2.x

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;

public static IActionResult Run(HttpRequest req, TraceWriter log)
{
   log.Info("C# HTTP trigger function processed a request.");

   if (req.Query.TryGetValue("name", out StringValues value))
   {
     return new OkObjectResult($"Hello, {value.ToString()}");
   }

   return new BadRequestObjectResult("Please pass a name on the query string");
}
Pragna Gopa
  • 726
  • 3
  • 10
0

In Functions v2 this has changed to req.GetQueryParameterDictionary();

Chris Ryan
  • 26
  • 2