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

Ton*_*ony 5 azure azure-functions

我有这个功能

[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()
    });
}
Run Code Online (Sandbox Code Playgroud)

问题是它正在返回

{"nome":"TONY","metodo":"GET"}
Run Code Online (Sandbox Code Playgroud)

我想让它回来

{"Nome":"TONY","Metodo":"GET"}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Hor*_*oma 5

你可以这样做:

[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")
        };
    }
Run Code Online (Sandbox Code Playgroud)