Search code examples
c#asp.netasp.net-coreasp.net-core-2.0asp.net-web-api-routing

Creating a Post Route with ASP.NET CORE 2.0 WebAPI parameter is null


I'm sending POST from PostMan: screenshot of postman settings And the parameter keeps coming up as null. From what I read, changing Post([FromBody]Models.Question value) and setting the model should be able to handle the json parameter coming in. I think I am missing a setting or I don't understand how to handle the json data correctly.

QuestionsController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace quiz_backend.Controllers
{
    [Produces("application/json")]
    [Route("api/Questions")]
    public class QuestionsController : Controller
    {
        // POST api/values
        [HttpPost]
        public void Post([FromBody]Models.Question value)
        {

        }
    }
}

Model is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace quiz_backend.Models
{
    public class Question
    {
        public string Text{ get; set; }
    }
}

Solution

  • The property on your model is Text while the request body property you are sending is "test". No wonder they will not bind and you get null. The case does not matter here but you have different word all together.