azure-functions - 从浏览器访问时,req.Content.ReadAsStringAsync().Result 始终返回 null(但在 Azure 门户中没有)
问题描述
下面包含我的完整代码(Azure 门户上的 Azure Function App)。请特别注意这两行。
var jsonContent = req.Content.ReadAsStringAsync().Result;
log.LogInformation("jsonContent" + jsonContent);
当我使用右侧面板下的请求正文测试该功能jsonContent
时,它会按原样打印在日志中。但是,在浏览器中使用函数 url并将其附加&name=azure
,jsonContent
则为 null,如日志中所示。
//full code
#r "Newtonsoft.Json"
using System.Net;
using Newtonsoft.Json;
using System.Text;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ILogger log)
{
// these two lines are problematic???
var jsonContent = req.Content.ReadAsStringAsync().Result;
log.LogInformation("jsonContent" + jsonContent);
// you can ignore the following lines (not related to question)
string jsonToReturn = "Hello World";
return new HttpResponseMessage(HttpStatusCode.OK) {
Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")
};
}
我尝试将线路更改为此,但它也没有工作。
var jsonContent = await req.Content.ReadAsStringAsync().Result;
错误类似于
'string' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
无论如何,我知道一个解决方法是使用HttpRequest
而不是HttpRequestMessage
生成jsonContent
,但我只是好奇为什么这个案例不起作用。
谁能发现我的错误?谢谢!
解决方案
推荐阅读
- python - 如何用python修改现有的Excel单元格颜色
- android - 无法过渡到片段。期望不同类型的函数
- java - UnsatisfiedLinkError:java.library.path 中没有 sqljdbc_auth
- laravel - 如何在脚本中设置 if else 语句
- html - 如何使 3d 立方体原地旋转
- javascript - 在服务工作者文件中使用 ESM(导入/导出)
- sql-server - MS SQL - 将表分离到另一个文件或文件组
- shell - 有什么作用。./build.man 在这段代码中是什么意思?
- asp.net-core - 在实体框架中使用 where 子句
- python - 如何在 python 3 中创建和启动任意数量的线程