Ali*_*zad 3 asp.net-mvc asp.net-core angular
我正在从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')
);
Run Code Online (Sandbox Code Playgroud)
ASP.NET
[HttpPost]
public IActionResult DemoAction(string firstName)
{
var req = Request;
return null;
}
Run Code Online (Sandbox Code Playgroud)
Request.Form具有类似{\"firstName\":\"Ali\"}但参数firstName为的形式的数据null
您尝试使用内容类型url编码形式发送JSON内容(使用JSON.stringify方法创建).
您应该尝试使用application/json one:
let body = JSON.stringify({ firstName: 'Ali' });
let headers = new Headers({ 'Content-Type': 'application/json' });
this.http.post(this.url, body, { headers: headers })
Run Code Online (Sandbox Code Playgroud)
编辑
如果您想提供表单内容,可以使用URLSearchParams该类:
var params = new URLSearchParams();
params.set('firstName', 'Ali');
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
this.http.post(this.url, params.toString(), { headers: headers })
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5129 次 |
| 最近记录: |