web api 2 中, 假如返回对象加了[serializable]特性,返回的json、xml数据都是包含对象的私有变量,而非公有属性,如:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19{
"Total": 1,
"Data": [
{
"_ticketnumber": "sample string 1",
"_onlinedate": "sample string 2",
"_name": "sample string 3",
"_price": "sample string 4",
"_verificationtime": "sample string 5",
"_type": "sample string 6",
"_authmethod": "sample string 7",
"_authaccount": "sample string 8",
"_authstores": "sample string 9",
"_source": "sample string 10",
"_uniqueid": "sample string 11",
"_createtime": "2015-08-15T14:14:40.8733315+08:00"
}
]
}
1 | <PageableModelOfOrderYGLG4_PHE xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/xxx.Api.DTOs"> |
这是因为加了[serialiable]特性的对象将会被序列化出所有的字段,包括private与public的,
要解决这个问题,我们可以移除这个特性,或者在WebApiConfig.cs中加入如下代码,以修改
序列化的方式:
1 | config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver(); |
如此,返回的json或xml包含的就都是共有属性了,同时我们也能发现,xml数据中针对不同对象
的命名空间属性xmlns也去掉了。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19{
"Total": 1,
"Data": [
{
"TicketNumber": "sample string 1",
"OnlineDate": "sample string 2",
"Name": "sample string 3",
"Price": "sample string 4",
"VerificationTime": "sample string 5",
"Type": "sample string 6",
"AuthMethod": "sample string 7",
"AuthAccount": "sample string 8",
"AuthStores": "sample string 9",
"Source": "sample string 10",
"UniqueID": "sample string 11",
"CreateTime": "2015-08-15T14:17:05.1900932+08:00"
}
]
}
1 | <PageableModelOfOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |