【发布时间】:2017-10-10 09:40:01
【问题描述】:
我有 Visual Studio 2017 15.3 并安装了 Azure 开发工作负载。
我根据这篇文章创建了一个空白HttpTrigger:
如果我使用Name作为参数查询字符串,我可以调试成功。
但是,如果我使用 Postman 创建此 Post 请求:
{
"name": "Azure"
}
我收到以下错误:
"mscorlib:执行函数时出现异常:HttpTrigger .匿名托管 DynamicMethods 程序集:'Newtonsoft.Json.Linq.JObject' doe s 不包含 'name' 的定义。”
这是我在 Visual Studio 2017 中的函数应用代码:
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
namespace VS2017TestFunctionApp
{
public static class HttpTrigger
{
[FunctionName("HttpTrigger")]
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.");
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
// Set name to query string or body data
name = 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);
}
}
}
另外,如果我在 Run 函数中复制完全相同的代码,并通过 Azure 门户在函数应用程序中使用相同的测试帖子,则一切正常。
【问题讨论】: