0

In our project there is a code style that force us to use model with property name like camelcase-style

public class MyModelClass { 
   public int CountryId { get; set; } 
   public int CountryName { get; set; } 
}

But service, that invoke out REST-API transfer HTTP-body with parameters like country_id and country_name. And I can't map the http-query to my model in controller action. Is there in ASP.NET CORE MVC way to map properties like that

public class MyModelClass { 
   [SpecialAttribute("country_id")]
   public int CountryId { get; set; } 

   [SpecialAttribute("country_name")]
   public int CountryName { get; set; } 
}

Or is there another way to achieve this?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • If the incoming http body json is being deserialized by Json.Net (which I think is the default in core) you can tell it to use snake case by default: https://www.newtonsoft.com/json/help/html/NamingStrategySnakeCase.htm – GC. Aug 08 '18 at 15:52

1 Answers1

0

uses to serialize and deserialize json. You can view the documentation here. That being said you can easily do what you want by simply using the JsonPropertyAttribute:

public class MyModelClass 
{ 
  [JsonProperty("country_id")]
  public int CountryId { get; set; } 

  [JsonProperty("country_name")]
  public int CountryName { get; set; } 
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150