0
我正在使用Nancy,並且我創建了一個帶List屬性的基本模型。當我使用下面的GET方法時,我得到了預期的JSON輸出。但是,當我嘗試POST回相同的JSON時,我最終得到一個帶有空字符串的對象。我創建了兩個屬性來驗證我的問題不是因爲在構造函數中將DataPoints參數實例化爲新的List。有沒有人有任何想法,爲什麼這不正確地返回?NancyFX Post - 使用列表參數綁定到對象
JSON對象從初始返回獲取
{
"dataPoints": [
"0",
"1",
"2",
"3",
"4"
],
"dataPoints2": [
"0",
"1",
"2",
"3",
"4"
]
}
JSON對象從後返回:
{
"dataPoints": [],
"dataPoints2": []
}
類:
public class BasicModel
{
private List<string> _dataPoints;
public List<string> DataPoints
{
get
{
if (_dataPoints == null)
{
_dataPoints = new List<string>();
}
return _dataPoints;
}
set
{
_dataPoints = value;
}
}
public List<string> DataPoints2 { get; set; }
public BasicModel()
{
DataPoints2 = new List<string>();
}
public BasicModel(int idx)
{
DataPoints2 = new List<string>();
for (int i = 0; i < idx; i++)
{
DataPoints.Add(i.ToString());
DataPoints2.Add(i.ToString());
}
}
}
獲取方法:
Get["/basicModel/{idx}"] = parameters =>
{
int idx = Convert.ToInt32(parameters["idx"].ToString());
BasicModel m = new BasicModel(idx);
return Negotiate
.WithStatusCode(HttpStatusCode.OK)
.WithModel(m)
.WithView("default");
};
POST方法:即使你給一個
Post["/basicmodel"] = parameters =>
{
BasicModel m = new BasicModel();
this.BindTo(m);
return Negotiate
.WithStatusCode(HttpStatusCode.OK)
.WithModel(m)
.WithView("default");
};