我的智慧在这里结束了。我想我只需要另一双眼睛。
方法签名:
public async Task<IHttpActionResult> Post(ApiRequest request)
型号:
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Global")]
[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Global")]
public class ApiRequest
{
[JsonProperty("allowLoadingToDataWarehouse")]
public bool AllowLoadingToDataWarehouse { get; set; }
[JsonProperty("initialDelay")]
public string InitialDelay { get; set; }
[JsonProperty("reportIds")]
public IEnumerable<string> ReportIds { get; set; }
[JsonProperty("reportTypeDelay")]
public string ReportTypeDelay { get; set; }
[JsonProperty("runType")]
[JsonConverter(typeof(StringEnumConverter))]
public ReportRunType RunType { get; set; }
[JsonProperty("userId")]
public string UserId { get; set; }
[JsonProperty("wwDelay")]
public string WWDelay { get; set; }
[JsonProperty("weeks")]
public IEnumerable<string> Weeks { get; set; }
}
使用Javascript:
var submitReportRequest = {
userId: userid,
reportIds: reportids,
runType: 'Custom',
weeks: selectedweeks,
initialDelay: $('#InitialDelay').val(),
reportTypeDelay: $('#ReportTypeDelay').val(),
wwDelay: $('#WWDelay').val(),
allowLoadingToDataWarehouse: $('#AllowLoadingToDataWarehouse').val()
};
$.post("/api/SubmitReport", JSON.stringify(submitReportRequest), function (data) {
alert('success');
});
来自JavaScript Post的序列化Json:
{
"userId": "30",
"reportIds": [
"59",
"60",
"61",
"62",
"63",
"64"
],
"runType": "Custom",
"weeks": [
"201409",
"201410",
"201411",
"201412"
],
"initialDelay": "00:00:00",
"reportTypeDelay": "00:00:00",
"wwDelay": "00:00:00"
}
最初我分别为Ids和Delays int
和TimeSpan
,并且没有正确反序列化。因此,我将它们全部更改为字符串,并且它们仍未正确反序列化。
我做错了什么或错过了什么?
编辑:在尝试了每个属性组合后,我最终决定将其粘贴到Fiddler Composer中。它有效。所以我的JavaScript必须要有一些东西。
答案 0 :(得分:1)
事实证明,当需要将post()
设置为Content-Type
application/x-www-form-urlencoded; charset=UTF-8
方法会将请求的application/json; charset=UTF-8
属性设置为id
我通过观看Chrome中的网络流量,并将我的javascript更改为此来找到 answer