Can't get query parameter from HttpRequestData
Asked Answered
R

5

27

I'm upgrading my code from .NET 3.0 to .NET 5.0, this changes the sintaxis quite a bit. In my previous code, which is a http request build in AZURE FUNCTIONS .NET 5.0 isolate, builds an GET api that takes parameters.

This is my previous code from .NET 3.0

using Microsoft.Azure.WebJobs; 
using Microsoft.Azure.WebJobs.Extensions.Http;


public static async Task<IActionResult> Run(
  [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
  ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");
    byte[] RSA_Key_to_Correct = new byte[0x80];
    string array_var = req.Query["array_var"];
    string i = req.Query["i"];
    string incrementing_value = req.Query["incrementing_value"];
}

I just cant find a way to use req to grab a parameter from the api call like it was done on .NET 3.0 string i = req.Query["i"];

In .NET 5.0 im using

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

Any hint?

Resolutive answered 22/6, 2021 at 16:50 Comment(5)
Are you looking for Request.QueryString("fullname")? (can't get't the square brackets right in the link). Intellisense reveals all the members. Use it!Flageolet
It wont work since im using Microsoft.Azure.Functions.Worker.Http; for .NET 5.0, this only works for .NET -4.8. For .NET 5.0 i suppose to be using HttpRequestDataResolutive
Did you look at the documentation for that class? Did that contain any information about the URL or the parameters?Baca
https://mcmap.net/q/505461/-string-array-input-from-query-string-in-az-function-net5 will answer your query. You should be using Microsoft.AspNetCore.WebUtilities.QueryHelpers package in .net 5 to get and parse query parameters.Hardtop
Don't you just love it when MS removes functionality for no reason!Afternoon
K
20

In Azure function .NET 5.0, we use the HttpRequestData in Http Trigger. The class does not contain Query parameter. For more details, please refer to here enter image description here

So if you want to get query string, you can use the package Microsoft.AspNetCore.WebUtilities.QueryHelpers to implement it as @user1672994 said.

For example

var queryDictionary = 
    Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(req.Url.Query);
var result = queryDictionary["<key name>"];
Kielty answered 23/6, 2021 at 1:19 Comment(1)
I think installing the additional ASP.NET Core package is redundant. One can simply use the following: System.Web.HttpUtility.ParseQueryString(req.Url.Query);, followed by var result = queryDictionary["<key name>"]; Bunco
P
27

There is a system package that gives the same result. That is probably why it was removed. Just use:

var query = System.Web.HttpUtility.ParseQueryString(req.Url.Query);
var from = query["key"]

This gives the same result as req.Query["array_var"];

Enjoy 😉

Penn answered 8/11, 2021 at 21:28 Comment(0)
K
20

In Azure function .NET 5.0, we use the HttpRequestData in Http Trigger. The class does not contain Query parameter. For more details, please refer to here enter image description here

So if you want to get query string, you can use the package Microsoft.AspNetCore.WebUtilities.QueryHelpers to implement it as @user1672994 said.

For example

var queryDictionary = 
    Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(req.Url.Query);
var result = queryDictionary["<key name>"];
Kielty answered 23/6, 2021 at 1:19 Comment(1)
I think installing the additional ASP.NET Core package is redundant. One can simply use the following: System.Web.HttpUtility.ParseQueryString(req.Url.Query);, followed by var result = queryDictionary["<key name>"]; Bunco
I
16

You can just add the the query parameter name to the function parameter list as follows and access the value:

public static async Task<HttpResponseData> Run(
      [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequestData req, 
      FunctionContext executionContext, string parameter1)
    {
        var log = executionContext.GetLogger("TestParam");
        log.LogInformation("C# HTTP trigger function processed a request.");
        log.LogInformation($"Parameter Value: {parameter1}");

    }
Incoherence answered 20/9, 2021 at 15:14 Comment(2)
Your answer isn't about the isolated process functions. The HttpRequest isn't available there.Gluteus
@Gluteus yes I made a mistake by copying the same code as in question. Thanks for pointing out. I have corrected the answer but still how I handled the query parameter is same and its applicate for isolated functions.Incoherence
B
6

If you are using Azure Functions (isolated) with .NET 5.0 - you can get it out of FunctionContext.BindingContext.BindingData like this:

functionContext.BindingContext
               .BindingData["weatherForecastId"]
               .ToString();
[Function("WeatherForecastGet")]
public async Task<HttpResponseData> Get([HttpTrigger(AuthorizationLevel.Function, "get", Route = "weather/{weatherForecastId:required}")] HttpRequestData req,
                                        FunctionContext executionContext)
{
    string weatherForecastId = executionContext.BindingContext
                                               .BindingData["weatherForecastId"]
                                               .ToString();

    var result = this.doSomething(weatherForecastId);
    
    var response = req.CreateResponse(HttpStatusCode.OK);
    await response.WriteAsJsonAsync(result);

    return response;
}

As the question asked about the HttpRequestData. The FunctionContext is also available in it. So you can achieve the same results (with more steps) like this:

httpRequestData.FunctionContext
               .BindingContext
               .BindingData["weatherForecastId"]
               .ToString();
[Function("WeatherForecastGet")]
public async Task<HttpResponseData> Get([HttpTrigger(AuthorizationLevel.Function, "get", Route = "weather/{weatherForecastId:required}")] HttpRequestData req,
                                        FunctionContext executionContext)
{
    string weatherForecastId = req.FunctionContext
                                  .BindingContext
                                  .BindingData["weatherForecastId"]
                                  .ToString();

    // some logic
}

This also works with the parameter in both query and path. As you can see both parameters in the API path below is in the BindingData

/api/weather/12?range=today

Debugger

Beguile answered 16/11, 2021 at 16:29 Comment(0)
S
-3

This works for me. Nice and simple. There is a method to get the query params into a dictionary.

var qp = req.GetQueryParameterDictionary();

var foobar = req["foobar"]; 
Spore answered 28/2, 2022 at 13:39 Comment(2)
This doens't seem to be available in Functions .NET 5 running in isolated mode.Gluteus
The question was about isolated process. You may want to consider deleting your answer.Pyrrha

© 2022 - 2024 — McMap. All rights reserved.