简体   繁体   中英

How can I bind model in ASP.NET MVC CORE with special attributes

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?

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

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