M
M
maxemga2021-06-20 22:53:31
ASP.NET
maxemga, 2021-06-20 22:53:31

How to send an object as a POST request in ASP.NET?

I have a controller:

[ApiController]
    [Route("api")]
    public class HomeController : ControllerBase
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        [HttpGet("login")]
        public IActionResult Get()
        {
            return Ok(new { id = 1 });
        }

        [HttpPost("logout")]
        public IActionResult Return()
        {
            return Ok(new { object });
        }
    }

I want to send a request and a JSON object on the client, for example { id: 1 }
But how can I accept it on the server? In Express, for example, this is req.body,how can I do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BasiC2k, 2021-06-20
@maxemga

In the Return() parameters, specify the object handle to be passed.
It can be:
Return(int id)
or a class describing json, for example:
[HttpPost]
[Produces("application/json")]
public IActionResult Return([FromBody] InPOST form)
where
public class InPOST
{
public int id { get ; set; }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question