C# 将数组发布到Azure函数HTTP触发器将返回500状态代码

C# 将数组发布到Azure函数HTTP触发器将返回500状态代码,c#,http,azure-functions,httpresponsemessage,C#,Http,Azure Functions,Httpresponsemessage,我有一个非常简单的Azure HTTP触发函数,它接收带有以下数据的POST: { "symbols": ["Azure1", "Azure2", "Azure3"] } 我的Azure功能是: #r "Newtonsoft.Json" using System.Net; using System.Linq; using System.Net.Http; using System.Net.Http.Formatting; using System.Net.Http.Headers; u

我有一个非常简单的Azure HTTP触发函数,它接收带有以下数据的
POST

{
    "symbols": ["Azure1", "Azure2", "Azure3"]
}
我的Azure功能是:

#r "Newtonsoft.Json"
using System.Net;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    // parse query parameter
    string symbols = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "symbol", true) == 0)
        .Value;

    // Get request body
    dynamic data = await req.Content.ReadAsAsync<object>();

    // Set name to query string or body data
    symbols = symbols ?? data?.symbols;

    return symbols == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, symbols, JsonMediaTypeFormatter.DefaultMediaType);
}

这个错误是有道理的。您正在将
符号
声明为
字符串
,但随后将
数据?.symbols
分配给它,它是一个数组。因此,消息
无法将类型“Newtonsoft.Json.Linq.JArray”隐式转换为“string”

除非您希望支持通过查询字符串传递数据,否则您应该去掉该查询字符串逻辑。e、 g.试试这个:

#r "Newtonsoft.Json"
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using Newtonsoft.Json.Linq;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    dynamic data = await req.Content.ReadAsAsync<object>();
    JArray symbols = data?.symbols;

    return symbols == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass symbols in the body")
        : req.CreateResponse(HttpStatusCode.OK, symbols, JsonMediaTypeFormatter.DefaultMediaType);
}
#r“Newtonsoft.Json”
Net系统;
使用System.Net.Http;
使用System.Net.Http.Formatting;
使用Newtonsoft.Json.Linq;
公共静态异步任务运行(HttpRequestMessage请求、TraceWriter日志)
{
动态数据=wait req.Content.ReadAsAsync();
JArray符号=数据?符号;
返回符号==null
?请求CreateResponse(HttpStatusCode.BadRequest,“请在正文中传递符号”)
:req.CreateResponse(HttpStatusCode.OK,symbols,JsonMediaTypeFormatter.DefaultMediaType);
}

我发现默认的自动生成C#函数有点让人困惑。它同时读取查询和正文,使用
对象
动态
——对于初学者来说,这不是一个很好的示例。F#也一样。谢谢@David。成功了。米哈伊尔,我完全同意!类型转换问题的一个次要话题。可能是默认模板确实令人困惑。它试图演示多种技术,这就是它同时查询字符串和正文的原因。但这确实让事情变得更复杂。我们可以把这件事带到其他地方进一步讨论。
#r "Newtonsoft.Json"
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using Newtonsoft.Json.Linq;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    dynamic data = await req.Content.ReadAsAsync<object>();
    JArray symbols = data?.symbols;

    return symbols == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass symbols in the body")
        : req.CreateResponse(HttpStatusCode.OK, symbols, JsonMediaTypeFormatter.DefaultMediaType);
}