为什么控制器响应将模型字段名称设置为小写? [英] Why the controller response are setting model field names into lower case?

查看:35
本文介绍了为什么控制器响应将模型字段名称设置为小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的.NET Core项目中,在所有控制器的响应中,对象字段以小写形式出现在字段名称的前一个或两个字母中:

In my .NET Core project, in the response of all controllers, the object fields are coming in lower case in the first one or two letters of the field name:

{
  "iD_PARAM": "foo",
  "cD_PROM": "bar",
  "txT_OFFICER": "lorem",
  "cN_NEW_PARAM": "fubá",
  "iD_SITUATION": "XX",
  "iD_NEW_USER": "ipsun",
}

这很奇怪,因为模型在大写情况下具有所有字段:

It's strange, because the model has all fields in UPPER case:

public partial class MyModel {
   public long ID_PARAM { get; set; }
   public long CD_PROM { get; set; }
   public string TXT_OFFICER { get; set; }
   public int CN_NEW_PARAM { get; set; }
   public int ID_SITUATION { get; set; }     
   public int ID_NEW_USER { get; set; }  
}

更多信息,这是我在其中设置值和响应的控制器:

For more detail, this is the controller where I set the values and the response:

[HttpPost("receive")]

    public async Task<IActionResult> Get()
    {
      try
      {
        MyModel newParam = new MyModel ();

        newParam.ID_PARAM = "foo";
        newParam.CD_PROM = "foo";
        newParam.TXT_OFFICER = "lorem";
        newParam.CN_NEW_PARAM = "fubá";
        newParam.ID_SITUATION = "XX";
        newParam.ID_NEW_USER = "ipsun";

        return Ok(newParam);
      }
      catch (Exception ex)
      {
        return BadRequest(ex);
      }
    }

推荐答案

假设您使用的是Newtonsoft Json,如果您希望Json属性为大写,请尝试使用JsonProperty装饰这样的模型,以防止序列化程序尝试推断出财产名称:

Assuming you are using Newtonsoft Json, if you want your Json properties to be uppercase, try decorating your Model with JsonProperty like this to prevent the Serializer try to infer the property name :

public partial class MyModel {
  [JsonProperty("ID_PARAM")]
  public long ID_PARAM { get; set; }
  [JsonProperty("CD_PROM")]
  public long CD_PROM { get; set; }
  [JsonProperty("TXT_OFFICER")]
  public string TXT_OFFICER { get; set; }
  [JsonProperty("CN_NEW_PARAM")]
  public int CN_NEW_PARAM { get; set; }
  [JsonProperty("ID_SITUATION")]
  public int ID_SITUATION { get; set; }     
  [JsonProperty("ID_NEW_USER")]
  public int ID_NEW_USER { get; set; }  
}

这篇关于为什么控制器响应将模型字段名称设置为小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆