How can I configure serialization of my Web API to use camelCase (starting from lowercase letter) property names instead of PascalCase like it is in C#. Can I do it globally for the whole project?
using System.Text.Json.Serialization;
public class Developer
{
[JsonPropertyName("Id")]
public int Id { get; set; }
[JsonPropertyName("FirstName")]
public string? FirstName { get; set; }
[JsonPropertyName("LastName")]
public string? LastName { get; set; }
[JsonPropertyName("RowNum")]
public int RowNum { get; set; }
[JsonPropertyName("StartDate")]
public DateTime StartDate { get; set; }
}
5条答案
按热度按时间jucafojl1#
If you want to change serialization behavior in Newtonsoft.Json aka JSON.NET, you need to create your settings:
You can also pass these settings into
JsonConvert.SerializeObject
:For ASP.NET MVC and Web API. In Global.asax:
Exclude null values:
Indicates that null values should not be included in resulting JSON.
ASP.NET Core
ASP.NET Core by default serializes values in camelCase format.
mwkjh3gx2#
For MVC 6.0.0-rc1-final
Edit Startup.cs, In the
ConfigureServices(IserviceCollection)
, modifyservices.AddMvc();
gr8qqesn3#
ASP.NET核心1.0.0 Json序列化具有默认camelCase.Referee this Announcement
xmjla07d4#
如果您想在更新的(vNext)C# 6.0中执行此操作,则必须通过
Startup.cs
类文件中ConfigureServices
方法中的MvcOptions
进行配置。tjvv9vkg5#
正如其他人所指出的,camelCase是默认的,但是如果你不想在任何地方都这样,你可以像这样注解你的类。