C# Hal-HttpPost始终发送空值
我在使用WebApi.Hal时遇到了HttpPosts问题。 我是这样发帖的:C# Hal-HttpPost始终发送空值,c#,json,asp.net-web-api,C#,Json,Asp.net Web Api,我在使用WebApi.Hal时遇到了HttpPosts问题。 我是这样发帖的: $.ajax({ type: "POST", url: "http://localhost/TestWebApi/api/test/test2/", data: { "reference": bookingref, "leadname": leadpax,
$.ajax({
type: "POST",
url: "http://localhost/TestWebApi/api/test/test2/",
data: {
"reference": bookingref,
"leadname": leadpax,
"_links": {}
},
contentType: "application/hal+json",
//dataType: "application/hal+json",
success: function(data, status) {
alert("POST: Data: " + data + "\nStatus: " + status);
$("#result").text(JSON.stringify(data, null, 4));
},
error: function(xhr, ajaxOptions, thrownError) {
alert('POST: Status: ' + xhr.status + ', Error Thrown: ' + thrownError);
}
});
public LoginDetails PostTest2([FromBody]LoginDetails value)
{
return new LoginDetails();
}
然后像这样拿起帖子:
$.ajax({
type: "POST",
url: "http://localhost/TestWebApi/api/test/test2/",
data: {
"reference": bookingref,
"leadname": leadpax,
"_links": {}
},
contentType: "application/hal+json",
//dataType: "application/hal+json",
success: function(data, status) {
alert("POST: Data: " + data + "\nStatus: " + status);
$("#result").text(JSON.stringify(data, null, 4));
},
error: function(xhr, ajaxOptions, thrownError) {
alert('POST: Status: ' + xhr.status + ', Error Thrown: ' + thrownError);
}
});
public LoginDetails PostTest2([FromBody]LoginDetails value)
{
return new LoginDetails();
}
LoginDetails继承自表示,但值始终为null。
有什么想法吗?已修复,最初我是这样安装Hal格式化程序的
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonHalMediaTypeFormatter());
GlobalConfiguration.Configuration.Formatters.Add(new XmlHalMediaTypeFormatter());
我把它改成这个
GlobalConfiguration.Configuration.Formatters.Insert(0, new XmlHalMediaTypeFormatter());
GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonHalMediaTypeFormatter());
因此,它保留了旧的格式化程序,但在默认格式化程序之前插入了Hal格式化程序(赋予它们更高的优先级)。LoginDetails看起来如何?你能确认PostTest2被点击了吗?如果它是一个
newLoginDetails()
,它将是空的。您正在返回一个新对象。如果LoginDetails与您通过ajax post传递的输入数据对象不匹配,则该对象将为null。