gpt4 book ai didi

c# - Azure 函数中没有 GetQueryNameValuePairs 的定义

转载 作者:太空狗 更新时间:2023-10-30 00:37:20 26 4
gpt4 key购买 nike

所有,我正在开发我的第一个 Azure Functions。该函数的目的是使用 Bing 认知 API 接收文本并对其进行拼写检查。但是,我无法编译,因为在我的代码中的 string text = req.GetQueryNameValuePairs()... 行,因为它指出 HTTPRequestMessage 不包含“GetQueryNameValuePairs”的定义,并且没有扩展方法“GetQueryNameValuePairs”接受第一个参数可以找到类型“HttpRequestMessage”(您是否缺少 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);
}
}
}

最佳答案

看起来您已经创建了 Azure Function v2(.Net Core)。 .Net Core/Standard 程序集中的 HttpRequestMessage 上没有这样的方法 GetQueryNameValuePairs,它在 .Net Framework 中可用。

快速修复方法是创建一个 v1(.NetFramework) 函数项目。如果你想保留v2功能,代码重构是必要的。

在 v2 Httptrigger 模板中,您可以看到 HttpRequest req(在灰色注释中),它使用 req.Query["name"] 来获取查询参数。当我们将 HttpRequestMessage 更改为 HttpRequest 时,还需要进行其他几项更改。此外,v1 中的 TraceWriter 也被废弃,v2 中我们使用 ILogger

关于c# - Azure 函数中没有 GetQueryNameValuePairs 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53293014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com