NetCore偶尔有用篇:NetCore项目WebApi返回Json属性大小写

一、概述


1、前面文章介绍Controller的大小写问题时,目的只是介绍它的差异性,有同学回复了,这里把它作为一个点写一下吧。

 

二、默认定义的转换结果


1、写一个返回对象的方法。

2、运行查看结果。

 

api方法如下

    public class OneController : Controller
    {
        public Model GetString(string id)
        {
            return new Model() { ID = id, Name = "aa" };
        }
    }

    public class Model
    {
        public string ID { get; set; }
        public string Name { get; set; }
    }

运行结果

 

NetCore偶尔有用篇:NetCore项目WebApi返回Json属性大小写_第1张图片

 三、自定义转换方法


1、添加Startup自定义转换代码。

2、重新运行查看结果。

 

添加转换

NetCore偶尔有用篇:NetCore项目WebApi返回Json属性大小写_第2张图片

 

NetCore偶尔有用篇:NetCore项目WebApi返回Json属性大小写_第3张图片

 

一般情况下,我们还是使用默认的比较好。

 

转载于:https://www.cnblogs.com/chenyinxin/p/9015712.html

你可能感兴趣的:(NetCore偶尔有用篇:NetCore项目WebApi返回Json属性大小写)