我试图调用一个方法,但参数显示为null
服务器端代码
[HttpPost]
public IHttpActionResult PostRule(ActionRuleParameter actionRule)
{
// SOME CODE
return BadRequest();
}
public class ActionRuleParameter
{
public string action;
public string rule;
}
客户端代码
addRule: function ($scope) {
//var data = { "action": "post", "rule": { "ID": "1", "Name": "Ramesh", "PassFail": "Pass" } }
var data1 = { "action": "post","rule":"rule" };
$http({
url: urlContent + '/api/Rules',
method: "POST",
data: { "actionRule": data1 }
}).success(function (response) {
$scope.rules = response;
});;
},
action
和rule
将显示为null。
答案 0 :(得分:1)
您需要在参数
之前添加[FromBody] public IHttpActionResult PostRule([FromBody]ActionRuleParameter actionRule)
{
// SOME CODE
return BadRequest();
}
答案 1 :(得分:0)
我使用Angular和新的ASP.NET堆栈来解决这个问题。添加[FromBody]为我修复了它,请尝试:
[HttpPost]
public IHttpActionResult PostRule([FromBody]ActionRuleParameter actionRule)
{
// SOME CODE
return BadRequest();
}