How can I post a string to an ASP.NET Core action method from Angular?
Here is what I am trying:
client side :
this.http.post(
"http://foo/bar/baz",
"foo",
{ responseType: "text" }
).pipe(
map(res => res)
);
server side :
[Route("bar")]
[ApiController]
public class FooController : BaseApiController
{
[Authorize]
[HttpPost("baz")]
[ProducesResponseType(typeof(string), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetStr(string str)
{
return GenerateResponseMessage(str);
}
}
Whenever I am getting to the return GenerateResponseMessage(str);
line using a breakpoint, the str
is null. While I am expecting the str
to be equal the "foo"
. What am I missing here?
Help me, please. I always thought that it is very easy to post
or get
anything with the help of Angular and ASP.NET Core, but it seems that the evil is in details, because I can not figure out the issue for too long.
I need to post
the string. I am aware of the get
option, but my specific case requires posting the string.
[Produces("application/json")]
[Route("api/bar")]
[ApiController]
public class FooController : ControllerBase
{
[HttpPost("baz")]
public async Task<ActionResult> Login([FromBody]string str)
this way you can solve
FromBody should do the trick:
public async Task<IActionResult> GetStr([FromBody] string str)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.