
ASP.NET Core Ver 3以降の仕様
ASP.NET Core のVer 3以降では、WEBAPI と MVCのフレームワークでは、オブジェクトをJSONに変換した際、キーの先頭が自動で小文字(Camel Casing)になってしまいます。
これはSystem.Text.Json の JsonSerializerクラスのシリアライズ/デシリアライズのデフォルト設定によるものです。
例えば、JSON シリアライズの対象となる以下のようなクラスがあったとします。
Mail と Name の各プロパティ名は大文字で設定されています。
しかし、JSON にシリアライズすると先頭文字が小文字になってしまいます。
public class Department
{
public string Mail{ get; set; }
public string Name{ get; set; }
}
{"mail":"******@gmail.com", "name":"Tarou"}
JsonSerializerOptions 設定
上記で解説したJSONシリアライズした際にキーの頭文字が小文字になってしまう (Camel Casing) 現象ですが、
JsonSerializerOptions の PropertyNamingPolicy 設定を以下のようにProgram.csに記述することで回避することが可能です。
services.AddControllersWithViews().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
ASP.NET おすすめ入門講座

3つのWebアプリケーションの開発を通して、ASP.NETについて基礎から学べるおすすめの入門講座がこちら☟
【入門者向け】ASP.NET MVCでWebアプリ開発のノウハウを学ぼう!
