ASP.NET
[HttpPost]
[Route("apitest")]
public string apitest([FromBody]string str)
{
Console.Writeline(str); // str is always null
return null;
}
Angular 2:
var creds = "str='testst'" ;
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
http.post('http://localhost:18937/apitest', creds, {
headers: headers
})
.map(res => res.json())
.subscribe(
(res2) => {
console.log('subsribe %o', res2)
}
);
我也试过 creds = {"str":"test"};
没有标题 JSON.stringify()
等没有成功。如何将数据发布到 ASP.NET?
最佳答案
var creds = {
str: 'testst'
};
$http.post('http://localhost:18937/apitest', JSON.stringify(creds));
Web API Controller 没有变化,它应该可以工作。
关于c# - 从 Angular 2 到 ASP.NET ApiController 的 Http.post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33739709/