I am new to Azure Functions and trying to convert .NET Core APIs to Azure Functions. The problem I am facing is how to globally set response naming convention (JSON). By default, it is CamelCase but I want to use PascalCase, all the solutions I found on internet were to modify the response in each function / endpoint. I would like to set it up globally
I had already added Startup.cs to configure DIs. Here is what I have tried in attempt to configure response JSON naming convention:
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddMvcCore().AddJsonOptions(m =>
{
m.JsonSerializerOptions.PropertyNamingPolicy = null;
});
builder.Services.Configure<JsonSerializerSettings>(m =>
{
m.ContractResolver = new DefaultContractResolver();
});
builder.Services.Configure<JsonSerializerOptions>(m =>
{
m.DictionaryKeyPolicy = null;
m.PropertyNamingPolicy = null;
});
builder.Services.Configure<JsonOptions>(m =>
{
m.JsonSerializerOptions.DictionaryKeyPolicy = null;
m.JsonSerializerOptions.PropertyNamingPolicy = null;
});
.....
}
Function Example (both return same response, which is CamelCased):
[FunctionName(nameof(Get))]
public async Task<IEnumerable<AppComponentViewModel>> Get([HttpTrigger(AuthorizationLevel.Function, "get", Route = "AppComponent/Get")] HttpRequest request)
{
var appComponents = await _appComponentRepository.GetAll();
return ToList(appComponents);
}
[FunctionName(nameof(Get))]
public async Task<ActionResult<IEnumerable<AppComponentViewModel>>> Get([HttpTrigger(AuthorizationLevel.Function, "get", Route = "AppComponent/Get")] HttpRequest request)
{
var appComponents = await _appComponentRepository.GetAll();
return Ok(ToList(appComponents));
}