.Net核心中Json()的小写属性名称

时间:2017-06-01 07:58:41

标签: c# .net json asp.net-core

有类ClientViewModel:

public class Result
{      
  public long PKID
  {
      get;
      set;
  }

   public string Name
   { 
      get; 
      set; 
   }
}

并使用post方法返回此模型:

[HttpPost]
    public JsonResult Search(string orderBy, List<Order> order, Search search,
        int start, int length, string searchTerm, bool isAsc, bool hideVoidedAndDeclined, bool recent = false)
    {
        var take = length;
        var skip = start;
        var m = _clientsService.Search(searchTerm, orderBy, hideVoidedAndDeclined, isAsc, take, skip, recent);


        return Json(m);
    }

但是当尝试在json中返回对象时,它看起来像:

name:"Nick22"
pkid: 5

但我需要保存寄存器

Name:"Nick22"
PKID: 5

1 个答案:

答案 0 :(得分:5)

应该更改合约清单。但由于你的外壳并不总是一样,你可能最好使用

services.AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); });