简体   繁体   中英

Change asp.net core model property name on runtime

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.

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; }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM