【发布时间】:2016-05-27 17:31:36
【问题描述】:
我正在从 Angular2 向 ASP.NET 5 控制器操作发起发布请求。 Angular 正确地发布数据并点击控制器操作,但它没有映射到控制器操作中定义的参数,参数为null。同时通过Request对象检查Request.Form具有正确的文本数据但未绑定到模型。
角度
let body = JSON.stringify({ firstName: 'Ali' });
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
this.http.post(this.url, body, { headers: headers })
.subscribe(
(data) => {
console.log('Response received');
console.log(data);
},
(err) => { console.log('Error'); },
() => console.log('Authentication Complete')
);
ASP.NET
[HttpPost]
public IActionResult DemoAction(string firstName)
{
var req = Request;
return null;
}
Request.Form 有{\"firstName\":\"Ali\"} 这样的形式的数据,但参数firstName 是null
【问题讨论】:
-
你为什么要“字符串化”json?只需发布 json 对象。
-
http.post的body参数需要是字符串
-
你试过直接发布数据吗?
-
是的,它是一样的。 firstName 参数为空,Request.Form 的值类似于 [object Object]
标签: asp.net-mvc asp.net-core angular