Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Lars Rasmussen 4 posts 34 karma points
    Jul 13, 2019 @ 12:34
    Lars Rasmussen

    [FromBody] return allways Null in Umbraco API controller

    When I upload a string (json) to the API I allways get Null.


                WebClient Client = new WebClient();
                Client.Encoding = System.Text.Encoding.UTF8;
                Client.Headers[HttpRequestHeader.ContentType] = "application/json; charset=UTF-8";
                Client.Headers[HttpRequestHeader.Accept] = "application/json";
                var mLink = "http://localhost:60760/Umbraco/Api/WebApi/CreateUser";
                dynamic json = new JObject();
                json.FirstName = "Lars";
                json.LastName = "Rasmussen";
                string mBack = Client.UploadString(mLink, "POST", json.ToString());


    public class WebApiController : UmbracoApiController
        // ~/Umbraco/Api/WebApi/CreateUser/
        public IEnumerable<WebApiCreateUser> CreateUser([FromBody] string JsonData)
           // JsonData is allways NULL why? Have do I get the json string?
            List<WebApiCreateUser> ls = new List<WebApiCreateUser>();
            ls.Add(new WebApiCreateUser() { Message = "Succes" });          
            return ls;
         }  }

    Thanks for any help :-)

  • Tor Langlo 151 posts 399 karma points
    Jul 13, 2019 @ 17:10
    Tor Langlo

    I'm not sure, but maybe the [FromBody] parameter needs to be a complex type compatible with the json passed from the client? I think ASP.NET Web Api will try to convert the json for you before your CreateUser method is called.

    So maybe create a private User class with FirstName and LastName properties and use that class as the type of the parameter.

    Otherwise, look at the client side, and what the json string looks like after you've done the JObject.ToString() and see if there are any surprises there.


  • Bjarne Fyrstenborg 1155 posts 3349 karma points MVP 4x c-trib
    Jul 13, 2019 @ 18:01
    Bjarne Fyrstenborg

    Hi Lars

    Does it work if you either define the parameter as dynamic as in this example or prefix the posted value with = as in these examples?

    You could also try creating a class with properties matching the json data:

    public IEnumerable<WebApiCreateUser> CreateUser([FromBody] User user) {


Please Sign in or register to post replies

Write your reply to: