在以下代码段中,我不明白“//this failed”下的代码与“//this works”下的代码(在创建 Azure 函数时作为样板代码提供)之间的区别。 注意:我意识到不需要强制转换为对象,但 ReadAsAsync 返回对象....
public static class FunctionTest
{
[FunctionName("TestFunction")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
//if nothing in the query parameter - lets' look into the body
if (name == null)
{
//this fails
dynamic abc = (object) @"{""name"":""test""}";
string test = abc?.name;
// this works
dynamic data = await req.Content.ReadAsAsync<object>();
name = data?.name;
}
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
}
相关分类