【发布时间】:2017-07-25 11:16:19
【问题描述】:
我有一个包含options 属性的element 类,选项类可以反过来更改其属性,例如可以有这两个
Element1 = {
"Id": "1",
"Options": {
"Printable": "true",
"StackOverflow": "great"
}
}
Element2 = {
"Id": "2",
"Options": {
"Question": "awsome",
"PropertyDiferent": "empty"
}
}
在 web api 我有这样的方法:
Public object Post ([FromBody] Element element)
{
SaveToMongo (element);
}
元素类:
Public class Element
{
Public dynamic options {get; set; }
Public string id {get; set; }
}
当我从 Mongo 拿起 Element 时,我没有任何问题。但是当我使用 Api web 的 post 方法发送它时,它不会像自 Mongo 以来那样在 expando 对象中反序列化自身。我怎么能在两端得到类似的行为?
编辑:我尝试将选项从动态更改为 newtonsoft JObject,但没有成功。现在选项也被保存了,但它们产生了一个我不想要的父亲。
"options" : {
"_t" : "Newtonsoft.Json.Linq.JObject, Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed",
"_v" : {
"padding" : {
"_t" : "JArray",
"_v" : [
{
"_t" : "JValue",
"_v" : [
]
},
{
"_t" : "JValue",
"_v" : [
]
},
{
"_t" : "JValue",
"_v" : [
]
},
{
"_t" : "JValue",
"_v" : [
]
}
]
},
"image" : {
"_t" : "JValue",
"_v" : [
]
},
"alt" : {
"_t" : "JValue",
"_v" : [
]
},
"url" : {
"_t" : "JValue",
"_v" : [
]
},
"width" : {
"_t" : "JValue",
"_v" : [
]
},
"backgroundColor" : {
"_t" : "JValue",
"_v" : [
]
},
"text" : {
"_t" : "JValue",
"_v" : [
]
}
}
},
属性更多,因为我直接从 mongo 中提取了示例。
【问题讨论】:
-
你能用 Newtonsoft.Json 的 JObject 代替动态的吗?
-
@PedroDrewanz 我要试试。因为这些天我的一些方法都失败了。
标签: c# mongodb post asp.net-web-api