Soft_API_Dev
Soft_API_Dev

Reputation: 373

Changing display name in Model to show in response

I am looking for a way to change the display Name of a property in Model so that the same would be reflect in the response body in swagger.

I tried using DisplayName attribute for the field but it doesn't seem to be working.

[DisplayName("Fucntion ID")]
public string FunctionId { get; set; }

[DisplayName("Function Description")]
public string FunctionDEscription { get; set; }

Image

Is there any other way to achieve this? Thanks in advance.

Upvotes: 2

Views: 2111

Answers (2)

Soft_API_Dev
Soft_API_Dev

Reputation: 373

I got the answer-

Used [JsonPropertyName("Fucntion ID")] instead of [DisplayName("Fucntion ID")]

Upvotes: 0

Rena
Rena

Reputation: 36685

If you use asp.net core 3.x and asp.net 5,you could use [JsonPropertyName("Fucntion ID")] which is in System.Text.Json.

If you use asp.net core 2.x,you could use [JsonProperty("Fucntion ID")] which is in Json.NET.

Reference:

https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonpropertynameattribute?view=netcore-3.1

https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm

Upvotes: 3

Related Questions