gpt4 book ai didi

azure - 如何在 Azure Functions (v2) 中以帕斯卡大小写形式返回 JSON?

转载 作者:行者123 更新时间:2023-12-03 23:55:27 25 4
gpt4 key购买 nike

我有这个功能

[FunctionName("json")]
public static JsonResult json
(
[HttpTrigger(AuthorizationLevel.Anonymous, new string[] { "POST", "GET", "DELETE", "PATCH", "PUT" })]
HttpRequest req,
TraceWriter log
)
{
return new JsonResult(new
{
Nome = "TONY",
Metodo = req.Method.ToString()
});
}

问题是它正在返回

{"nome":"TONY","metodo":"GET"}

我希望它返回

{"Nome":"TONY","Metodo":"GET"}

在 ASP.Net Core 2 中我使用了这个:

services.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver());
// Keep the Case as is

如何配置 Azure Functions 来做到这一点?

最佳答案

你可以这样做:

[FunctionName("json")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequestMessage req, TraceWriter log)
{
return new HttpResponseMessage(HttpStatusCode.OK)
{
RequestMessage = req,
Content = new StringContent(
content: JsonConvert.SerializeObject(new
{
Nome = "TONY",
Metodo = req.Method.ToString()
}),
encoding: System.Text.Encoding.UTF8,
mediaType: "application/json")
};
}

关于azure - 如何在 Azure Functions (v2) 中以帕斯卡大小写形式返回 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49331537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com