.NET6 WebAPI默认出传到前台自动转化成小驼峰写法,如何设置某个controller返回值为大驼峰(我问的时单个设置某个controller返回值为大驼峰,不是全局都设置为大驼峰)
好文!学习了
builder.Services.AddControllers().AddJsonOptions("old", options => {});
public class JsonSettingsNameAttribute : Attribute { }
[JsonSettingsName("old")]
public class OldApiController : ControllerBase {}
}
public static string? GetJsonSettingsName(this HttpContext context)
{
return context.GetEndpoint()
?.Metadata
.GetMetadata<JsonSettingsNameAttribute>()
?.Name;
}
public override bool CanRead(InputFormatterContext context)
{
if (context.HttpContext.GetJsonSettingsName() != SettingsName)
return false;
return base.CanRead(context);
}
public override bool CanWriteResult(OutputFormatterCanWriteContext context)
{
if (context.HttpContext.GetJsonSettingsName() != SettingsName)
return false;
return base.CanWriteResult(context);
}
public class ConfigureMvcJsonOptions : IConfigureOptions<MvcOptions> {}
builder.Services.AddSingleton<IConfigureOptions<MvcOptions>>(sp =>
{
var options = sp.GetRequiredService<IOptionsMonitor<JsonOptions>>();
var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
return new ConfigureMvcJsonOptions(settingsName, options, loggerFactory);
});
options.JsonSerializerOptions.PropertyNamingPolicy = null;//区分大小写