locked
Why I can't get the string in the webapi? RRS feed

  • Question

  • User1052024640 posted

    I created this webapi as below:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    
    // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
    
    namespace WebApplication1.Controller
    {
        [Route("api/[controller]")]
        public class ValuesController : Microsoft.AspNetCore.Mvc.Controller
        {
            // GET: api/<controller>
            //[HttpGet]
            //public IEnumerable<string> Get()
            //{
            //    return new string[] { "value1", "value2" };
            //}
    
            // GET api/<controller>/5
            [HttpGet()]
            public IActionResult Get(string inputvalue)
            {
                return Ok("what you said is:"+inputvalue);
            }       
            // POST api/<controller>
            [HttpPost]
            public IActionResult Post([FromBody]string inputvalue)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }            
                return CreatedAtAction("Get", inputvalue);
            }
    
            // PUT api/<controller>/5
            [HttpPut("{id}")]
            public void Put(int id, [FromBody]string value)
            {
            }
    
            // DELETE api/<controller>/5
            [HttpDelete("{id}")]
            public void Delete(int id)
            {
            }
        }
    }
    

    I wanna whenever I post "How do you do", then the browser returns "what you said is: How do you do".

    I followed the tutorial. But after I added a breakpoint I found that the IActionResult Get never works.

    I want to upload the screenshot of Postman which using to test the webapi. However, It seems the forums cannot upload any image. In addition, the Postman only returns the string "how do you do?"

    What's wrong with the code and how can I achieve it? Thank you.

    Friday, June 21, 2019 9:11 AM

Answers

  • User-854763662 posted

    Hi mywatermelon ,

    mywatermelon

    Emmm...I am a beginner and it seems is differ much from the original method by using the default Post.

    Although it works I don't know why it works and what's wrong with my code.

    CreatedAtAction Method creates a CreatedAtActionResult object that produces a Status201Created response.

    CreatedAtAction( ) essentially creates a "fake" response, filling the body with the same response object that Post was given .

    Make the following modification :

    [HttpGet()]
    public IActionResult Get(string value)
    {
           var response = "what you said is:" + value;
           return Ok(response);
    }
    
    // POST api/<controller>
    [HttpPost]
    public IActionResult Post([FromBody]string inputvalue)
    {
           if (!ModelState.IsValid)
           {
                return BadRequest(ModelState);
           }
           var response = "what you said is:" + inputvalue;
           return CreatedAtAction("Get", new { value = inputvalue }, response);
    }

    Reference : https://stackoverflow.com/questions/47039224/how-to-get-ef-to-either-not-save-children-or-load-children-after-a-post-save/47041474#47041474 

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 24, 2019 7:30 AM

All replies

  • User-1038772411 posted

    WebApi Controller:

     [HttpGet]
            [Route("api/Getrecord/{inputvalue}")]
            [ResponseType(typeof(string))]
            public HttpResponseMessage Getrecord(string inputvalue)
            {
                return Request.CreateResponse(HttpStatusCode.OK, "what you said is:" + inputvalue);
            }

    MVC Controller :

     [HttpGet]
            public ActionResult Login()
            {
                var str = "How do you do";
                var orgdata = GlobalVariables.WebApiClient.GetAsync($"Getrecord/{str.ToString()}").Result;
                var orgresult = orgdata.Content.ReadAsAsync<string>().Result;
                var strdata = orgresult;
    }

    Please use this code instead of your you will get whole string "what you said is:How do you do" in mvc response.

    Thanks

    Friday, June 21, 2019 9:54 AM
  • User1052024640 posted

    Emmm...I am a beginner and it seems is differ much from the original method by using the default Post.

    Although it works I don't know why it works and what's wrong with my code.

    Friday, June 21, 2019 10:01 AM
  • User-1038772411 posted

    What you don't understand in my code, i will explain you. event this is simple webapi calling in mvc project.

    I don't have your mvc code may you have something wrong when calling webapi. kindly refer code from my pattern,it will help you more.

    Thanks.

    Friday, June 21, 2019 10:11 AM
  • User-854763662 posted

    Hi mywatermelon ,

    mywatermelon

    Emmm...I am a beginner and it seems is differ much from the original method by using the default Post.

    Although it works I don't know why it works and what's wrong with my code.

    CreatedAtAction Method creates a CreatedAtActionResult object that produces a Status201Created response.

    CreatedAtAction( ) essentially creates a "fake" response, filling the body with the same response object that Post was given .

    Make the following modification :

    [HttpGet()]
    public IActionResult Get(string value)
    {
           var response = "what you said is:" + value;
           return Ok(response);
    }
    
    // POST api/<controller>
    [HttpPost]
    public IActionResult Post([FromBody]string inputvalue)
    {
           if (!ModelState.IsValid)
           {
                return BadRequest(ModelState);
           }
           var response = "what you said is:" + inputvalue;
           return CreatedAtAction("Get", new { value = inputvalue }, response);
    }

    Reference : https://stackoverflow.com/questions/47039224/how-to-get-ef-to-either-not-save-children-or-load-children-after-a-post-save/47041474#47041474 

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 24, 2019 7:30 AM
  • User1052024640 posted

    I know now, thank you so much. You are the best answer.

    Monday, June 24, 2019 10:14 AM