如何在 API 负载响应中获取友好的属性名称?
How to get a friendly attribute name in API payload response?
我希望获取请求 return 一个友好的名称而不是实际的数据库列名称。可能吗?
我的模型class
public class Employee
{
public int EmployeeID { get; set; }
[DisplayName("First Name")]
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string Phone { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}
生成的负载。请注意,名字未更改
{
"employeeID": 1501,
"firstName": "Syed Omar",
"lastName": "Khan",
"phone": "9234567891",
"email": "Syed Omar@yahoo.com",
}
如果您想更改 json 属性 序列化 class 的名称,您可以根据使用的 json 库使用相应的属性。对于 System.Text.Json
,它将是 JsonPropertyNameAttribute
。对于 Newtonsoft 的 Json.NET - JsonPropertyAttribute
:
[JsonPropertyName("First Name")]
public string FirstName { get; set; } = string.Empty;
或者
[JsonProperty("First Name")]
public string FirstName { get; set; } = string.Empty;
我是通过像下面这样修饰 属性 名字得到的
[JsonPropertyName("Employee ID")]
public int EmployeeID { get; set; }
[JsonPropertyName("First Name")]
public string FirstName { get; set; } = string.Empty;
我希望获取请求 return 一个友好的名称而不是实际的数据库列名称。可能吗?
我的模型class
public class Employee
{
public int EmployeeID { get; set; }
[DisplayName("First Name")]
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string Phone { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}
生成的负载。请注意,名字未更改
{
"employeeID": 1501,
"firstName": "Syed Omar",
"lastName": "Khan",
"phone": "9234567891",
"email": "Syed Omar@yahoo.com",
}
如果您想更改 json 属性 序列化 class 的名称,您可以根据使用的 json 库使用相应的属性。对于 System.Text.Json
,它将是 JsonPropertyNameAttribute
。对于 Newtonsoft 的 Json.NET - JsonPropertyAttribute
:
[JsonPropertyName("First Name")]
public string FirstName { get; set; } = string.Empty;
或者
[JsonProperty("First Name")]
public string FirstName { get; set; } = string.Empty;
我是通过像下面这样修饰 属性 名字得到的
[JsonPropertyName("Employee ID")]
public int EmployeeID { get; set; }
[JsonPropertyName("First Name")]
public string FirstName { get; set; } = string.Empty;