Api Post Requist with single Paramater

osyris 236 Reputation points
2021-08-21T18:42:40.777+00:00

This would probably be a very easy problem to solve but i dont seem to udnerstand it.

Client-side:
axios.post("https://localhost:44332/api/Products/CreateCategory",name)

backend:

[HttpPost("CreateCategory")]
        public async Task<IActionResult> CreateCategory(String name)
        {

if try that or this:

[HttpPost("CreateCategory")]
        public async Task<IActionResult> CreateCategory([FromBody] string name)
        {

it does not work but if i do this:

[HttpPost("CreateCategory")]
        public async Task<IActionResult> CreateCategory(CategoryDto dto)
        {

model:

    public class CategoryDto
    {
        public string Name { get; set; }
    }

it works perfectly

How can use post request without the use of models.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,165 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2021-08-22T02:20:54.58+00:00

    Hi @osyris ,

    To send a request with single parameter, you could try to use the following method:

    1. Send parameter via query string.

    Append the parameter at the end of the request url, code as below:

            //send parameter via query string  
            var name = "Tom";  
            var res = axios.post("https://localhost:44310/api/ToDo/CreateCategory?name=" + name);  
    

    Api Controller:

        [Route("api/[controller]")]  
        [ApiController]  
        public class TodoController : ControllerBase  
        {   
            [HttpPost("CreateCategory")]  
            public async Task<IActionResult> CreateCategory(string name)  
            {  
                return Ok("Success");  
            }  
    

    The result as below:

    125304-3.gif

    2. Send parameter via Form

    Code like this:

            const resp = axios.post('https://localhost:44310/api/ToDo/CreateCategory', 'name=world');    
    

    Api Controller:

        [Route("api/[controller]")]  
        [ApiController]  
        public class TodoController : ControllerBase  
        {   
            [HttpPost("CreateCategory")]  
            public async Task<IActionResult> CreateCategory([FromForm]string name)  
            {  
                return Ok("Success");  
            }  
    

    The result like this:

    125294-5.gif


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    1 person found this answer helpful.
    0 comments No comments

  2. Duane Arnold 3,211 Reputation points
    2021-08-23T21:33:19.85+00:00

    You need to understand Encapsulation and information hiding.

    https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)

    That's what a DTO, a class, provides is encapsulation. The 'name' data can have data that can make the URL malformed when passing data. The DTO prevents the URL from becoming malformed.

    https://learn.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5

    0 comments No comments