‘HttpRequestMessage’ Does Not Contain A Definition For ‘GetQueryNameValuePairs’

In this azure tutorial, we will discuss how to fix the error, ‘HttpRequestMessage’ Does Not Contain A Definition For ‘GetQueryNameValuePairs’. This error I got after executing the Azure Function Code to interact with the Sharepoint From Azure Function using the Visual Studio 2019.

‘HttpRequestMessage’ Does Not Contain A Definition For ‘GetQueryNameValuePairs’

Recently, I was trying to write one Azure Function to connect to Share Point and to delete a SharePoint list. I came across with few lines of code that are not supported with the latest version of Azure Function.

Getquerynamevaluepairs Example

Below was my function code

using System;
using System.IO;
using System.Threading.Tasks;
//using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
//using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;
using System.Net;
using System.Net.Http;
//using Microsoft.AspNetCore.Mvc.WebApiCompatShim;
using System.Linq;

namespace deletesplistvs
{
    public static class Function1
    {
        [FunctionName("NewFunction")]
        public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage request,
            ILogger log)
        {

            string site = "https://tsinfo.sharepoint.com/sites/SPFx/";
            string userName = "[email protected]";
            string password = "####@###5";

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


            System.Security.SecureString secureString = new System.Security.SecureString();
            foreach (char ch in password)
            {
                secureString.AppendChar(ch);
            }

When I started executing the code, I go the below error

‘HttpRequestMessage’ Does Not Contain A Definition For ‘GetQueryNameValuePairs’

Below is the Complete error message

Error CS1061 ‘HttpRequestMessage’ does not contain a definition for ‘GetQueryNameValuePairs’ and no accessible extension method ‘GetQueryNameValuePairs’ accepting a first argument of type ‘HttpRequestMessage’ could be found (are you missing a using directive or an assembly reference?) deletesplistvs C:\Users\Bijay\source\repos\deletesplistvs\deletesplistvs\Function1.cs 32

You can able to see the error as below

request.getquerynamevaluepairs
getquerynamevaluepairs

‘HttpRequestMessage’ Does Not Contain A Definition For ‘GetQueryNameValuePairs’ [Solved]

To fix the error ‘HttpRequestMessage’ Does Not Contain A Definition For ‘GetQueryNameValuePairs’, I am sorry to say that you have to downgrade the Azure Function version in your .csproj file.

Open your Azure Function project file i.e Projectname.csproj and do the below changes

<PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <AzureFunctionsVersion>v1</AzureFunctionsVersion>
  </PropertyGroup>

Below are the changes as highlighted

Error CS1061 'HttpRequestMessage' does not contain a definition for 'GetQueryNameValuePairs'
httprequestmessage getquerynamevaluepairs

Now if you will run the Project you will not get the error

Error CS1061 'HttpRequestMessage' does not contain a definition for 'GetQueryNameValuePairs' and no accessible extension method
task httpresponsemessage does not contain a definition for content

But, Personally I feel again going back to the older version of the Azure Function is not at all a good Idea and looks a bit uneasy You can try using the below way

You can use the HttpRequest req as the parameter and can use in the following way

string name = req.Query["name"];
public static class MyProductHTTPFunction
    {
        [FunctionName("MyProductHTTPFunction")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            log.LogInformation("MyProductHTTPFunction function processed a request.");

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

In this way you can fix the error ‘HttpRequestMessage’ Does Not Contain A Definition For ‘GetQueryNameValuePairs’.

No Definition for GetQueryNameValuePairs in Azure Function

If you have created the Azure Function project V2 (.Net Core), Then, you will not get the methods like GetQueryNameValuePairs on the HttpRequestMessage in .Net Core, It is actually available in the .Net Framework version.

The best way to fix this issue is to create a V1(.NetFramework Azure Function project), If your requirement is you have to stick to V2 (.Net Core) Azure Function project then, you have to refactor your total code and you can refer to the above section for the updated code details.

Getquerynamevaluepairs

Getquerynamevaluepairs method helps you to get the parsed query string as a collection of key-value pairs. It belongs to the System.Net.Http namespace.

Syntax:

The syntax of the Getquerynamevaluepairs method is as below.

public static IEnumerable<KeyValuePair<string, string>> GetQueryNameValuePairs(
    this HttpRequestMessage request
)

Here, the request parameter is nothing but the HTTP request message.

Gets the parsed query string as a collection of key-value pairs.

You may also like following the below articles

Wrapping Up

Well, in this article, we discussed, How to fix the error ‘HttpRequestMessage’ Does Not Contain A Definition For ‘GetQueryNameValuePairs’. Hope it will help you to fix your issue !!!