我正在尝试从我的最小 API 中获取结果,该 API 在我的 MVC Web 应用程序的端点中配置,我的 Get 操作配置如下:
endpoints.MapGet(
"HO-CFDZU4/api/Currency/Get",
[PermissionAuthorize(PermissionName.ReadCurrencyDictionary)]
async ([FromServicesAttribute] CurrencyService curency) =>
{
var result = await DataSourceLoader.LoadAsync(curency.Get(), new DataSourceLoadOptions());
return Results.Ok(result);
});
Run Code Online (Sandbox Code Playgroud)
结果我得到了对象的响应,其中属性名称更改为小写,并且它不适合我。我想在相同的情况下获得完全相同的名称,就像我返回表单操作一样。
为了在 MVC 中获得类似的效果,我使用了以下代码:
services
.AddMvc()
.AddFluentValidation(x => x.RegisterValidatorsFromAssembly(AppDomain.CurrentDomain.GetAssemblies().Where(x => x.FullName.Contains("ApplicationCore")).Single()))
.AddMvcLocalization()
.AddMvcOptions(options =>{})
.AddRazorRuntimeCompilation()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
Run Code Online (Sandbox Code Playgroud)
在控制器中使用操作时,它为 Json 设置属性命名策略,但我不知道如何为minimalApi 设置相同的策略。
我尝试过的是设置[JsonPropertyName(name)]它运行良好,但我们有很多课程,我正在寻找更全局的解决方案。
我还尝试像这样全局配置 JsonOptions:
services.Configure<JsonOptions>(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
Run Code Online (Sandbox Code Playgroud)
但它什么也不做