c# - 从 Angular 2 到 ASP.NET ApiController 的 Http.post

标签 c# asp.net angular post

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/

相关文章:

c# - 如何查找字符串中的两个引号

angular - OnScroll 事件不起作用 - Angular Dart

html - 是使用 [innerHTML]、angular 4 进行数据绑定(bind)的最佳方式吗

javascript - 如何在我的项目中找到我的 Angular 版本?

c# - 为什么 DOM(文档对象模型)将所有内容加载到主内存中

c# - 此 SqlParameterCollection 不包含 ParameterName 为 '@UserId' 的 SqlParameter

c# - Request.IsAuthenticated 如何工作?

c# - 带有更新查询的 OleDbCommand 会导致 OleDbException

c# - 编写一个通用方法来替换一系列遗留 API 方法

c# - 在 ASP.NET MVC 中使用即弃即弃的危险