Sahil Sharma
Sahil Sharma

Reputation: 4217

How to rename mapping from json being accepted in c# as body parameter?

[HttpPost]
public IHttpActionResult Post([FromBody]Stock stock)

public class Stock
    {
        public int DealerId { get; set; }
        public int StockId { get; set; }
        public long Kms { get; set; }
        public DateTime MfgYear { get; set; }
    }

Sample JSON request:

{  
   "stock": 
      {  
         "DealerId ":234,
         "StockId ":123,
         "Kms":12324,
         "versionId":987,
         "MfgYear":2010,
     }
}

I need to change c# Stock class variable names i.e. from Kms to Kilometer, MfgYear to ManufacturingYear etc.

When i am posting data from postman to my Stock api, I need to have same parameters in c# as i have it in my json. Is there any way i can change this mapping that would allow me to map json Kms to C# Kilometer?

Upvotes: 0

Views: 189

Answers (1)

Waldemar
Waldemar

Reputation: 5513

Simply use the JsonPropertyAttribute:

[JsonProperty(PropertyName = "Kms")]
public int Kilometer { get; set; }

Upvotes: 2

Related Questions