【发布时间】:2019-08-18 20:34:11
【问题描述】:
我的 C# post 方法看起来
[HttpPost]
public void mymethod(string id) {...}
我正在尝试像这样从角度 http.post 发布字符串值,但值到达 C# 方法时为 null。
const mystr = 'mystring';
this.http.post('myurl', mystr);
我尝试了什么
{id: mystr}
JSON.stringify({id: mystr})
注意:使用[HttpGet("{id}")] 发送 id 值效果很好,但 mystr 可能包含 '/' 值,因此只要包含 '/' 就会失败。
当我将我的方法签名替换为([FromBody] string id) 时,我得到400 Bad Request。
【问题讨论】:
-
使用:
this.http.post('myurl/'+mystr); -
mystr 可能包含 '/' 所以它不会一直工作。
-
然后将其传递到请求正文中,如下所述!
-
如果你不想声明一个类,那么进入
Request-Headers -
你能展示一下
mystr的样子吗?