我有这个功能
[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功能以这种方式?
你可以这样做:
[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")
};
}
将JsonPropertyAttribute添加到属性中,并通过文件顶部的#r“Newtonsoft.Json”包含Json.NET。
#r "Newtonsoft.Json"
using Newtonsoft.Json;
并装饰属性
[JsonProperty(PropertyName = "nome" )]
public string Nome { get; set; }
[JsonProperty(PropertyName = "metodo" )]
public string Metodo { get; set; }