ASP.NET 5控制器方法无法接收JSON POST,ASP.NET 4控制器方法却可以
我一直在把头发剪掉。我有两个项目,一个运行 ASP.NET 4
,另一个运行 ASP.NET 5 RC1
I have been tearing my hair out over this one. I have two projects one running ASP.NET 4
and the other ASP.NET 5 RC1
ASP.NET 5
项目控制器接收到的 POST
方法输入参数均为默认值
The ASP.NET 5
project controller received POST
method input parameters are all default and not the values as sent from the webpage.
为了缩小问题的范围,我简化了控制器中的 POST
方法两个项目
To narrow down the problem I simplified the controllers POST
methods in both projects
[HttpPost]
public JsonResult DataHandler(int Draw)
{
//above Draw variable not set
}
并在捕获方法上设置一个断点变量 Draw
。该网页发送 Draw
参数的值为1的JSON帖子。但是,在ASP.NET 5中,该值为0(默认值),并且我发送的其他参数为空而不是具有值。在 ASP.NET 4中是正确的。
and put a break point on the method to catch the variable Draw
. The webpage sends a JSON post with a value of 1 for the Draw
parameter. However in ASP.NET 5 that values is 0 (default) and other parameters I send are null instead of having values. In 'ASP.NET 4' it is correct.
我正在使用 jquery数据表
和与所使用的相同的代码在此 ASP.NET 4
项目
I am using jquery datatables
and the same code as used in this ASP.NET 4
project
var oTable = $('#datatab').DataTable({
"serverSide": true,
"ajax": {
"type": "POST",
"url": '/Home/DataHandler',
"contentType": 'application/json; charset=utf-8',
'data': function (data) { return data = JSON.stringify(data); }
},
"dom": 'frtiS',
"scrollY": 500,
"scrollX": true,
"scrollCollapse": true,
"scroller": {
loadingIndicator: false
},
"processing": true,
"paging": true,
"deferRender": true,
"columns": [
{ "data": "Name" },
{ "data": "City" },
{ "data": "Postal" },
{ "data": "Email" },
{ "data": "Company" },
{ "data": "Account" },
{ "data": "CreditCard" }
],
"order": [0, "asc"]
});
我使用了 Fiddler
并比较了 JSON 和 JSON
内容都发布到 / Home / DataHandler
两者完全相同,即 Draw = 1
。
I used Fiddler
and compared the JSON
sent by both project to the controller and the JSON
content posted to the /Home/DataHandler
for both are the exact same i.e. Draw=1
.
{ draw:1, columns:[{ data: Name, name:, searchable:true, orderable:true, search:{ value:, regex:false}},{ data: City, name:, searchable :true, orderable:true, search:{ value:, regex:false}},{ data: Postal, name:, searchable: true, orderable:true, search:{ value:, regex:false}},{ data: Email, name:, searchable:true, orderable:true, search:{ value:, regex:false}},{ data: Company, name:, searchable:true, orderable :true, search:{ value:, regex:false}},{ data: Account, name:, searchable:true, orderable: true, search:{ value:, regex:false}},{ data: CreditCard, name:, searchable:true, orderable:true, search:{ value:, regex:false}}], order:[{ column:0, dir: asc}], start:0, length:126, search:{ value:, regex:false}}
{"draw":1,"columns":[{"data":"Name","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}},{"data":"City","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}},{"data":"Postal","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}},{"data":"Email","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}},{"data":"Company","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}},{"data":"Account","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}},{"data":"CreditCard","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}}],"order":[{"column":0,"dir":"asc"}],"start":0,"length":126,"search":{"value":"","regex":false}}
我尝试过的事情。
- 我使用了相同的
html表
内容和上面的代码.js
项目之间的文件 - 将我的控制器输入参数设置为小写,例如
绘制
- 比较
JSON
POST 提琴手中的code>数据是相同的
- 在
POST
方法输入变量上放置一个断点以尽快捕获该值
- I used the same
html table
contents and above code.js
file between projects - Set my controller input parameter to lowercase e.g.
draw
- Compare the
JSON
POST
data in fiddler is the same - Put a breakpoint on the
POST
method input variable to catch the value as soon as it is posted
添加 dataType:'json'
到ajax调用
将 return data = JSON.stringify(data)
替换为以下:
Replace return data = JSON.stringify(data)
with the following:
return data.Draw = JSON.stringify(data)