Haridev Nirgude
Haridev Nirgude

Reputation: 43

ASP.Net WebAPI app post data always null for json

Despite of including jsonformatter in global.asax file, the post method could not parse the json data as Model and always showing null.

var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
            jsonFormatter.UseDataContractJsonSerializer = true;

I am using Fiddler to post the data and below is raw request:

POST http://localhost:50121/api/Store HTTP/1.1
User-Agent: Fiddler
Host: localhost:50121
Content-Type: "application/json"
Content-Length: 84

{”quantity”:"4",“imagePath”:”http://localhost/”,“price”:"20.00"}

Model:

namespace StoreBackEnd.Models
{
    public class Subcategory
    {
        public string quantity { get; set; }
        public string imagePath { get; set; }
        public string price { get; set; }


       }
}

The Post method is:

public HttpResponseMessage Post(Subcategory products)
    {
        var database = productsClient.GetDatabase("test");
        //IMongoCollection<Subcategory> collection = database.GetCollection<Subcategory>("entities");
        //collection.InsertOne(products);
        return new HttpResponseMessage() { StatusCode = HttpStatusCode.Accepted };
    }

The products variable of Subcategory is always null. I also tried using [FromBody] attribute but still variable is always null.

Can anyone suggest what am I missing? as the json also is well formatted.

Upvotes: 0

Views: 260

Answers (1)

Kyle Huang
Kyle Huang

Reputation: 451

The "double quotes" in your JSON don't look right.. Try following:

{"quantity":"4","imagePath":"http://localhost/","price":"20.00"}

Upvotes: 2

Related Questions