Mine is a .net core web api which returns user details: I have a model class like this:
public class UserDetails
{
[JsonProperty("name")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[JsonProperty("lastName")]
public string LastName { get; set; }
[JsonProperty("designation")]
public string Designation { get; set; }
}
I have an method in Data base Layer which fetches some user details from Mongo and de-serializes the json and casts into UserDetails
type.
When I see the response of the end point in postman, I see it like this:
> {
> "name": "ABC",
> "LastName": "XYZ",
> "designation": "team Lead"
> }
The Display Name which I mentioned as annotation on top of the property is not working.
How can I make my code return First Name instead of "name". Many thanks for all the answers.
DisplayAttribute doesn't work as you're expecting.
You're building a .NET Core Web Api, when the DisplayAttribute is only used in UI's - therefore .NET MVC projects.
In order to achieve what you want, there are a couple of possibilities:
public class UserDetailsDTO
{
[JsonProperty("first name")]
public string FirstName { get; set; }
[JsonProperty("lastName")]
public string LastName { get; set; }
[JsonProperty("designation")]
public string Designation { get; set; }
}
If you're using System.Text.Json, I don't think Microsoft has this feature available. At least, I found a couple of open issues on GitHub asking for this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.