Azure Functions V4 JSON序列化属性名自动转驼峰/小写,如何匹配C#原大小写?
解决Azure Functions V4 Http触发函数JSON属性名大小写匹配问题
原因
Azure Functions V4 默认使用 System.Text.Json 处理JSON序列化,默认启用驼峰命名策略,会自动转换C#属性名的大小写,导致输出的JSON属性名与类定义不一致。
解决方案
1. 全局配置(推荐,所有函数生效)
针对In-Process模型,在Startup.cs或Program.cs中配置JSON序列化选项,禁用自动大小写转换:
方式1:使用Startup.cs
using Microsoft.Azure.Functions.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.Mvc; [assembly: FunctionsStartup(typeof(YourNamespace.Startup))] namespace YourNamespace { public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { builder.Services.AddMvcCore() .AddJsonOptions(options => { // 禁用驼峰命名,保留原始属性大小写 options.JsonSerializerOptions.PropertyNamingPolicy = null; }); } } }
方式2:使用Program.cs(.NET 6+ Top-level语句)
using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; var host = Host.CreateDefaultBuilder(args) .ConfigureFunctionsWorkerDefaults(builder => { builder.Services.Configure<JsonOptions>(options => { options.JsonSerializerOptions.PropertyNamingPolicy = null; }); }) .Build(); host.Run();
2. 局部配置(单个函数生效)
如果只需在特定函数中保留原始大小写,可手动序列化对象并返回ContentResult:
using System.Text.Json; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; public static class YourFunction { [FunctionName("YourHttpTrigger")] public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req) { // 构造你的对象 var yourModel = new YourModel { Contacts = new List<Contact> { new Contact { firstName = "dummy" } }, Tags = new Tags { GROUPCONTACT = "dummy" } }; // 配置序列化选项 var jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true // 可选,格式化JSON输出 }; // 手动序列化并返回 var jsonString = JsonSerializer.Serialize(yourModel, jsonOptions); return new ContentResult { Content = jsonString, ContentType = "application/json", StatusCode = StatusCodes.Status200OK }; } } // 你的模型类 public class YourModel { public string JobId { get; set; } public List<Contact> Contacts { get; set; } public Tags Tags { get; set; } } public class Contact { public string firstName { get; set; } } public class Tags { public string GROUPCONTACT { get; set; } }
3. 逐个属性指定名称(适合个别属性调整)
通过[JsonPropertyName]特性为每个属性指定JSON输出的名称,精准匹配大小写:
using System.Text.Json.Serialization; public class YourModel { [JsonPropertyName("JobId")] public string JobId { get; set; } [JsonPropertyName("Contacts")] public List<Contact> Contacts { get; set; } [JsonPropertyName("Tags")] public Tags Tags { get; set; } } public class Contact { [JsonPropertyName("firstName")] public string firstName { get; set; } } public class Tags { [JsonPropertyName("GROUPCONTACT")] public string GROUPCONTACT { get; set; } }
验证
配置完成后,重新运行函数,输出的JSON属性名将与C#类中的属性名大小写完全一致,符合你的期望结果。
内容的提问来源于stack exchange,提问作者Tech with Thiru
相关产品推荐
相关产品推荐

