我想在ASP.Net Core 1.0中发送带有多个参数的请求(我不知道这是否重要)。
我有以下模型:
[DataContract]
public class RegistrationConfigContract
{
[DataMember]
public string ServiceType { get; set; }
[DataMember]
public int BatchTimeout { get; set; }
[DataMember]
public int BatchSize { get; set; }
[DataMember]
public ActorFinishingTypeEnum FinishingType { get; set; }
}
我有以下控制器方法:
public void SendRegistration(string nodeName, NodeTypeEnum nodeType, RegistrationConfigContract model)
{
}
我也有一个JS试图调用它:
function sendRegistration(nodeName, nodeType) {
var model = {
ServiceType: $("#serviceType").html(),
BatchTimeout: $("#batchTimeout").html(),
BatchSize: $("#batchSize").html(),
FinishingType: $("#finishingType").html()
};
var request = {
nodeName: nodeName,
nodeType: nodeType,
model: model
}
//$.post('@Url.Action("SendRegistration")', request);
$.ajax({
url: '@Url.Action("SendRegistration")',
type: "POST",
dataType: 'json',
data: JSON.stringify(request),
async: false,
cache: false,
traditional: true,
contentType: 'application/json'
});
}
但是根据Chrome / Edge调试器,在服务器端,我总是得到null,而在JS端,一切都很好。
我在这里做错了什么?我尝试使用通过Google搜索的代码来解决此问题,但是它不起作用。
原始HTTP请求:
POST http://localhost:8080/Configuration/SendRegistration HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
Accept: application/json, text/javascript; q=0.01
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Referer: http://localhost:8080/Configuration
Content-Length: 156
X-Compress: 1
Proxy-Authorization: 6e9b34bd44817cf5c254e1ccb4fe7b31ecd526ea9e025e06a16baca626af6be0ea7fa102c2205f0e
Connection: keep-alive
{"nodeName":"zhdp","nodeType":"DocumentProducer","model":{"ServiceType":"jjjjjj","BatchTimeout":"33535","BatchSize":"6666","FinishingType":"UpdateMessage"}}
问题在于模型绑定... ASP.NET Core中的模型绑定是不同的。您应该使用[FromBody]或[FromForm]等适当的属性标记动作的参数。另外,您既可以发布普通表单,也可以发布Ajax,但不能同时使用两者。
更多信息在这里:https : //andrewlock.net/model-binding-json-posts-in-asp-net-core/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句