1

I have a model

 public class Item
 {
    public int Id { get; set; }
    public string ItemName { get; set; }
 }

i like to show the Property ItemName as ServiceName in api end point as well as in swagger. Where user will get/post the data as ServiceName but in the back end it will bind to ItemName. Other important think is ServiceName should be dynamic, It come from database, where if i change ServiceName to ProductName then it should bind ProductName to ItemName. I am using asp.net core 2.2 with EF core and swagger ui.

2
  • Post your trying Commented Nov 21, 2019 at 5:23
  • sorry, i don't know what to do. I didn't try anything .. although i did see some custom model binding tutorials .. if that helps .. Commented Nov 21, 2019 at 5:28

1 Answer 1

2

You can use the JsonProperty attribute. It has a constructor that takes a propertyName string.

using Newtonsoft.Json;

public class Item
{
    public int Id { get; set; }

    [JsonProperty("ServiceName")]
    public string ItemName { get; set; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

it solve the first part of my problem, but did not solve the dynamic part ...
If you need to change "ServiceName" to "ProductName" then just change to [JsonProperty("ProductName")].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.