No Definition for GetQueryNameValuePairs in Azure Function












1















All,
I'm working on my first Azure Function. The intent of the function is to take in text and spell check it using the Bing cognitive API. However, I am unable to compile because at the string text = req.GetQueryNameValuePairs()... line in my code because it states HTTPRequestMessage does not contain a definition for 'GetQueryNameValuePairs' and no extension method 'GetQueryNameValuePairs' accepting a first argument of type 'HttpRequestMessage' could be found (are you missing a using directive or an assembly reference?).



Any help would be appreciated.



using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using System.Net;
using System;
using System.Net.Http;
using System.Linq;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace SpellCheck.Functions
{
public static class SpellCheck
{
[FunctionName("SpellCheck")]
//public async static Task Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");

//List<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>();
//values.Add(new KeyValuePair<string, string>("text", text));

//error here
string text = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "text", true) == 0)
.Value;

dynamic data = await req.Content.ReadAsAsync<object>();
text = text ?? data?.text;

// Replace the accessKey string value with your valid access key. - https://www.codeproject.com/Articles/1221350/Getting-Started-with-the-Bing-Search-APIs
const string accessKey = "MY_ACCESS_KEY_GOES_HERE";

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", accessKey);

// The endpoint URI.
const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/spellcheck?";
const string uriMktAndMode = "mkt=en-US&mode=proof&";

HttpResponseMessage response = new HttpResponseMessage();
string uri = uriBase + uriMktAndMode;

List<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>();
values.Add(new KeyValuePair<string, string>("text", text));

using (FormUrlEncodedContent content = new FormUrlEncodedContent(values))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
response = await client.PostAsync(uri, content);
}

string client_id;
if (response.Headers.TryGetValues("X-MSEdge-ClientID", out IEnumerable<string> header_values))
{
client_id = header_values.First();
}

string contentString = await response.Content.ReadAsStringAsync();

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









share|improve this question





























    1















    All,
    I'm working on my first Azure Function. The intent of the function is to take in text and spell check it using the Bing cognitive API. However, I am unable to compile because at the string text = req.GetQueryNameValuePairs()... line in my code because it states HTTPRequestMessage does not contain a definition for 'GetQueryNameValuePairs' and no extension method 'GetQueryNameValuePairs' accepting a first argument of type 'HttpRequestMessage' could be found (are you missing a using directive or an assembly reference?).



    Any help would be appreciated.



    using System.IO;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Azure.WebJobs.Host;
    using Newtonsoft.Json;
    using System.Net;
    using System;
    using System.Net.Http;
    using System.Linq;
    using System.Collections.Generic;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;

    namespace SpellCheck.Functions
    {
    public static class SpellCheck
    {
    [FunctionName("SpellCheck")]
    //public async static Task Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
    {
    log.Info("C# HTTP trigger function processed a request.");

    //List<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>();
    //values.Add(new KeyValuePair<string, string>("text", text));

    //error here
    string text = req.GetQueryNameValuePairs()
    .FirstOrDefault(q => string.Compare(q.Key, "text", true) == 0)
    .Value;

    dynamic data = await req.Content.ReadAsAsync<object>();
    text = text ?? data?.text;

    // Replace the accessKey string value with your valid access key. - https://www.codeproject.com/Articles/1221350/Getting-Started-with-the-Bing-Search-APIs
    const string accessKey = "MY_ACCESS_KEY_GOES_HERE";

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", accessKey);

    // The endpoint URI.
    const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/spellcheck?";
    const string uriMktAndMode = "mkt=en-US&mode=proof&";

    HttpResponseMessage response = new HttpResponseMessage();
    string uri = uriBase + uriMktAndMode;

    List<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>();
    values.Add(new KeyValuePair<string, string>("text", text));

    using (FormUrlEncodedContent content = new FormUrlEncodedContent(values))
    {
    content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
    response = await client.PostAsync(uri, content);
    }

    string client_id;
    if (response.Headers.TryGetValues("X-MSEdge-ClientID", out IEnumerable<string> header_values))
    {
    client_id = header_values.First();
    }

    string contentString = await response.Content.ReadAsStringAsync();

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









    share|improve this question



























      1












      1








      1








      All,
      I'm working on my first Azure Function. The intent of the function is to take in text and spell check it using the Bing cognitive API. However, I am unable to compile because at the string text = req.GetQueryNameValuePairs()... line in my code because it states HTTPRequestMessage does not contain a definition for 'GetQueryNameValuePairs' and no extension method 'GetQueryNameValuePairs' accepting a first argument of type 'HttpRequestMessage' could be found (are you missing a using directive or an assembly reference?).



      Any help would be appreciated.



      using System.IO;
      using Microsoft.AspNetCore.Mvc;
      using Microsoft.Azure.WebJobs;
      using Microsoft.Azure.WebJobs.Extensions.Http;
      using Microsoft.AspNetCore.Http;
      using Microsoft.Azure.WebJobs.Host;
      using Newtonsoft.Json;
      using System.Net;
      using System;
      using System.Net.Http;
      using System.Linq;
      using System.Collections.Generic;
      using System.Net.Http.Headers;
      using System.Threading.Tasks;

      namespace SpellCheck.Functions
      {
      public static class SpellCheck
      {
      [FunctionName("SpellCheck")]
      //public async static Task Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
      public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
      {
      log.Info("C# HTTP trigger function processed a request.");

      //List<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>();
      //values.Add(new KeyValuePair<string, string>("text", text));

      //error here
      string text = req.GetQueryNameValuePairs()
      .FirstOrDefault(q => string.Compare(q.Key, "text", true) == 0)
      .Value;

      dynamic data = await req.Content.ReadAsAsync<object>();
      text = text ?? data?.text;

      // Replace the accessKey string value with your valid access key. - https://www.codeproject.com/Articles/1221350/Getting-Started-with-the-Bing-Search-APIs
      const string accessKey = "MY_ACCESS_KEY_GOES_HERE";

      HttpClient client = new HttpClient();
      client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", accessKey);

      // The endpoint URI.
      const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/spellcheck?";
      const string uriMktAndMode = "mkt=en-US&mode=proof&";

      HttpResponseMessage response = new HttpResponseMessage();
      string uri = uriBase + uriMktAndMode;

      List<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>();
      values.Add(new KeyValuePair<string, string>("text", text));

      using (FormUrlEncodedContent content = new FormUrlEncodedContent(values))
      {
      content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
      response = await client.PostAsync(uri, content);
      }

      string client_id;
      if (response.Headers.TryGetValues("X-MSEdge-ClientID", out IEnumerable<string> header_values))
      {
      client_id = header_values.First();
      }

      string contentString = await response.Content.ReadAsStringAsync();

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









      share|improve this question
















      All,
      I'm working on my first Azure Function. The intent of the function is to take in text and spell check it using the Bing cognitive API. However, I am unable to compile because at the string text = req.GetQueryNameValuePairs()... line in my code because it states HTTPRequestMessage does not contain a definition for 'GetQueryNameValuePairs' and no extension method 'GetQueryNameValuePairs' accepting a first argument of type 'HttpRequestMessage' could be found (are you missing a using directive or an assembly reference?).



      Any help would be appreciated.



      using System.IO;
      using Microsoft.AspNetCore.Mvc;
      using Microsoft.Azure.WebJobs;
      using Microsoft.Azure.WebJobs.Extensions.Http;
      using Microsoft.AspNetCore.Http;
      using Microsoft.Azure.WebJobs.Host;
      using Newtonsoft.Json;
      using System.Net;
      using System;
      using System.Net.Http;
      using System.Linq;
      using System.Collections.Generic;
      using System.Net.Http.Headers;
      using System.Threading.Tasks;

      namespace SpellCheck.Functions
      {
      public static class SpellCheck
      {
      [FunctionName("SpellCheck")]
      //public async static Task Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
      public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
      {
      log.Info("C# HTTP trigger function processed a request.");

      //List<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>();
      //values.Add(new KeyValuePair<string, string>("text", text));

      //error here
      string text = req.GetQueryNameValuePairs()
      .FirstOrDefault(q => string.Compare(q.Key, "text", true) == 0)
      .Value;

      dynamic data = await req.Content.ReadAsAsync<object>();
      text = text ?? data?.text;

      // Replace the accessKey string value with your valid access key. - https://www.codeproject.com/Articles/1221350/Getting-Started-with-the-Bing-Search-APIs
      const string accessKey = "MY_ACCESS_KEY_GOES_HERE";

      HttpClient client = new HttpClient();
      client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", accessKey);

      // The endpoint URI.
      const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/spellcheck?";
      const string uriMktAndMode = "mkt=en-US&mode=proof&";

      HttpResponseMessage response = new HttpResponseMessage();
      string uri = uriBase + uriMktAndMode;

      List<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>();
      values.Add(new KeyValuePair<string, string>("text", text));

      using (FormUrlEncodedContent content = new FormUrlEncodedContent(values))
      {
      content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
      response = await client.PostAsync(uri, content);
      }

      string client_id;
      if (response.Headers.TryGetValues("X-MSEdge-ClientID", out IEnumerable<string> header_values))
      {
      client_id = header_values.First();
      }

      string contentString = await response.Content.ReadAsStringAsync();

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






      c# azure azure-functions






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 5:06









      Jerry Liu

      9,8581728




      9,8581728










      asked Nov 14 '18 at 4:00









      jgorayajgoraya

      498




      498
























          1 Answer
          1






          active

          oldest

          votes


















          3














          Looks like you have created an Azure Function v2(.Net Core). There's no such method GetQueryNameValuePairs on HttpRequestMessage in .Net Core/Standard assembly, it's available in .Net Framework.



          The quick fix would be create a v1(.NetFramework) Function Project. If you want to stay with v2 function, code refactor is necessary.



          In a v2 Httptrigger template, you can see HttpRequest req(in your grey comment) and it uses req.Query["name"] to get query parameter. There're several other changes necessary as we changed HttpRequestMessage to HttpRequest. Besides, TraceWriter in v1 is also abandoned, in v2 we use ILogger.






          share|improve this answer
























          • Thanks, I'll give the refactor a shot.

            – jgoraya
            Nov 14 '18 at 12:06











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293014%2fno-definition-for-getquerynamevaluepairs-in-azure-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Looks like you have created an Azure Function v2(.Net Core). There's no such method GetQueryNameValuePairs on HttpRequestMessage in .Net Core/Standard assembly, it's available in .Net Framework.



          The quick fix would be create a v1(.NetFramework) Function Project. If you want to stay with v2 function, code refactor is necessary.



          In a v2 Httptrigger template, you can see HttpRequest req(in your grey comment) and it uses req.Query["name"] to get query parameter. There're several other changes necessary as we changed HttpRequestMessage to HttpRequest. Besides, TraceWriter in v1 is also abandoned, in v2 we use ILogger.






          share|improve this answer
























          • Thanks, I'll give the refactor a shot.

            – jgoraya
            Nov 14 '18 at 12:06
















          3














          Looks like you have created an Azure Function v2(.Net Core). There's no such method GetQueryNameValuePairs on HttpRequestMessage in .Net Core/Standard assembly, it's available in .Net Framework.



          The quick fix would be create a v1(.NetFramework) Function Project. If you want to stay with v2 function, code refactor is necessary.



          In a v2 Httptrigger template, you can see HttpRequest req(in your grey comment) and it uses req.Query["name"] to get query parameter. There're several other changes necessary as we changed HttpRequestMessage to HttpRequest. Besides, TraceWriter in v1 is also abandoned, in v2 we use ILogger.






          share|improve this answer
























          • Thanks, I'll give the refactor a shot.

            – jgoraya
            Nov 14 '18 at 12:06














          3












          3








          3







          Looks like you have created an Azure Function v2(.Net Core). There's no such method GetQueryNameValuePairs on HttpRequestMessage in .Net Core/Standard assembly, it's available in .Net Framework.



          The quick fix would be create a v1(.NetFramework) Function Project. If you want to stay with v2 function, code refactor is necessary.



          In a v2 Httptrigger template, you can see HttpRequest req(in your grey comment) and it uses req.Query["name"] to get query parameter. There're several other changes necessary as we changed HttpRequestMessage to HttpRequest. Besides, TraceWriter in v1 is also abandoned, in v2 we use ILogger.






          share|improve this answer













          Looks like you have created an Azure Function v2(.Net Core). There's no such method GetQueryNameValuePairs on HttpRequestMessage in .Net Core/Standard assembly, it's available in .Net Framework.



          The quick fix would be create a v1(.NetFramework) Function Project. If you want to stay with v2 function, code refactor is necessary.



          In a v2 Httptrigger template, you can see HttpRequest req(in your grey comment) and it uses req.Query["name"] to get query parameter. There're several other changes necessary as we changed HttpRequestMessage to HttpRequest. Besides, TraceWriter in v1 is also abandoned, in v2 we use ILogger.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 5:06









          Jerry LiuJerry Liu

          9,8581728




          9,8581728













          • Thanks, I'll give the refactor a shot.

            – jgoraya
            Nov 14 '18 at 12:06



















          • Thanks, I'll give the refactor a shot.

            – jgoraya
            Nov 14 '18 at 12:06

















          Thanks, I'll give the refactor a shot.

          – jgoraya
          Nov 14 '18 at 12:06





          Thanks, I'll give the refactor a shot.

          – jgoraya
          Nov 14 '18 at 12:06


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293014%2fno-definition-for-getquerynamevaluepairs-in-azure-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()