Ниже приведен мой полный код (приложение-функция Azure на портале Azure).Обратите особое внимание на эти две строки.
var jsonContent = req.Content.ReadAsStringAsync().Result;
log.LogInformation("jsonContent" + jsonContent);
Когда я проверяю функцию, используя тело запроса под панелью справа, печатается jsonContent
в логах как положено.Однако, используя функцию url в браузере и добавляя ее с &name=azure
, jsonContent
является нулевым, как показано в журналах.
//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
, но мне просто любопытно, почему дело не работает.
Может кто-нибудь заметить мою ошибку?Спасибо!