代码之家  ›  专栏  ›  技术社区  ›  jgoraya

Azure函数中没有GetQueryNameValuePairs的定义

  •  3
  • jgoraya  · 技术社区  · 7 年前

    我正在开发我的第一个Azure函数。该函数的目的是使用Bing认知API接收文本并对其进行拼写检查。但是,我无法编译,因为在字符串text=req.GetQueryNameValuePairs()。。。因为它声明HTTPRequestMessage不包含“GetQueryNameValuePairs”的定义,并且找不到接受类型为“HTTPRequestMessage”的第一个参数的扩展方法“GetQueryNameValuePairs”(是否缺少using指令或程序集引用?)。

    任何帮助都将不胜感激。

    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);    
            }
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  11
  •   Jerry Liu Phantom    7 年前

    看起来您已经创建了一个Azure函数v2(.Net Core)。没有这种方法 GetQueryNameValuePairs 在…上 HttpRequestMessage

    快速修复方法是创建v1(.NetFramework)函数项目。如果您想继续使用v2函数,代码重构是必要的。

    在v2httptrigger模板中,您可以看到 HttpRequest req (在你的灰色评论中)它使用 req.Query["name"] 获取查询参数。随着我们的改变,还有其他一些必要的改变 httprequest消息 HttpRequest . 此外 TraceWriter 在v1中也被放弃,在v2中我们使用 ILogger .

        2
  •  5
  •   Community Mohan Dere    6 年前

    名字 姓氏 ,则可以尝试以下操作:

        var qs = req.RequestUri.ParseQueryString();
    
        string firstName = qs.Get("firstname");
        string lastName = qs.Get("lastname");