gpt4 book ai didi

c# - 读取请求的内容 - ReadAsStringAsync()

转载 作者:行者123 更新时间:2023-12-05 00:59:18 26 4
gpt4 key购买 nike

我尝试像这样读取请求的内容:

var翻译=await req.Content.ReadAsStringAsync();

但出现此异常:

enter image description here

Severity Code Description Project File Line Suppression State Error CS1061 'HttpRequest' does not contain a definition for 'Content' and no accessible extension method 'Content' accepting a first argument of type 'HttpRequest' could be found (are you missing a using directive or an assembly reference?)

这是我正在使用的库:

enter image description here

我做错了什么? 如何阅读内容正文?

这是周围的代码:

    public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "OnTranslateSingleHttpTriggered")] HttpRequest req,
ILogger log)
{
var translation = await req.Content.ReadAsStringAsync();
//do work
}

最佳答案

正如 @Garr 提到的,Content 是 HttpRequestMessage 的一个属性。在针对.NET Core 2的v2 Functions中,我们通常使用HttpRequest并读取如下内容。

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

更新

Runtime 2.0.12265已向VS用户开放,请放心使用.NET Core 2.2。

<小时/>

另请注意,自 runtime v2.0.12265 起支持 .NET Core 2.2但运行时更新尚未在所有地方推出,即我们仍然在本地使用旧版本。所以恢复Microsoft.AspNetCore.Mvc包裹至 2.1.0否则我们可能会得到错误。 ( Microsoft.AspNetCore.HttpMicrosoft.NET.Sdk.Functions 引用,因此无需再次安装)

您的项目文件(右键单击项目,Edit <FunctionProjectName>.csproj)应如下所示

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>

关于c# - 读取请求的内容 - ReadAsStringAsync(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54189988/

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