locked
Posted value to API is always null, or getting an error RRS feed

  • Question

  • So here's my code; the value is always null no matter what I do, and by adding the [System.Web.Mvc.HttpPost] decoration I now get an error stating that the route doesn't support posts.  I need some help.

    // POST api/values
            [System.Web.Mvc.HttpPost]
            public string Post(string value)
            {
                return value;
            }

    Here are the data I'm posting via Postman:

    {"name":"John Doe", "age":18, "country":"United States of America"}

    • Moved by CoolDadTx Tuesday, January 8, 2019 8:34 PM ASP.NET related
    Monday, January 7, 2019 9:38 PM

All replies

  • Hi,

    have you tried this:

    [Route("api/controller/method")]
    [System.Web.Mvc.HttpPost]
    public string Post([FromBody]string value)
    {
        return value;
    }

    Greetings, Chris

    @MrTaylor or someone else with privilege: This should be moved...


    • Edited by DerChris88 Monday, January 7, 2019 10:54 PM
    Monday, January 7, 2019 10:53 PM
  • Hi there, I tried and I got the same problem:  "The requested resource does not support http method 'POST'.<VMessage><\Error>"

    Tuesday, January 8, 2019 3:18 PM
  • I'm sorry, that was a new error.  I changed the code to:

    [System.Web.Http.HttpPost]
            public string Post([FromBody]string value)
            {
                return value;
            }

    and the value of "value" is always null, no matter what I do.

    Tuesday, January 8, 2019 4:07 PM
  • WebAPI can be discussed at the WebAPI forum in ASP.NET forums.

    https://forums.asp.net/

    Tuesday, January 8, 2019 6:57 PM