【发布时间】:2020-11-18 03:41:37
【问题描述】:
我是 Azure Functions 的新手,正在尝试将 .NET Core API 转换为 Azure Functions。我面临的问题是如何全局设置响应命名约定(JSON)。默认情况下,它是CamelCase,但我想使用PascalCase,我在互联网上找到的所有解决方案都是修改每个函数/端点中的响应。我想全局设置
我已经添加了Startup.cs 来配置 DI。这是我尝试配置响应 JSON 命名约定的尝试:
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;
});
.....
}
函数示例(都返回相同的响应,即 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));
}
【问题讨论】:
标签: c# azure .net-core json.net azure-functions