简体   繁体   中英

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.

JSON中的“双引号”看起来不正确。请尝试以下操作:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM