【发布时间】:2018-08-08 15:13:22
【问题描述】:
在我们的项目中,有一种代码风格迫使我们使用带有属性名称的模型,例如 camelcase-style
public class MyModelClass {
public int CountryId { get; set; }
public int CountryName { get; set; }
}
但是调用 REST-API 的服务会使用以下参数传输 HTTP-body
country_id 和 country_name。而且我无法在控制器操作中将 http-query 映射到我的模型。是否有 ASP.NET CORE MVC 方式来映射这样的属性
public class MyModelClass {
[SpecialAttribute("country_id")]
public int CountryId { get; set; }
[SpecialAttribute("country_name")]
public int CountryName { get; set; }
}
或者有其他方法可以实现吗?
【问题讨论】:
-
如果传入的 http body json 正在被 Json.Net 反序列化(我认为这是核心中的默认值),您可以告诉它默认使用蛇形大小写:newtonsoft.com/json/help/html/NamingStrategySnakeCase.htm
标签: c# asp.net-core-mvc json-serialization