I have a controller with a POST method that always receives the null parameter. I'm sending JSON through postman.
I have tried the class as a parameter, I have already tried it by string as a parameter and in both cases it is leaving it null;
Follow the controller:
using API_Shop.AppComponents.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace API_Shop.Controllers
{
public class TesteController : ApiController
{
// GET: api/Teste
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET: api/Teste/5
public string Get(int id)
{
return "value";
}
// POST: api/Teste
public void Post([FromBody]string value)
{
Teste tt = new Teste();
tt = JsonConvert.DeserializeObject<Teste>(value);
throw new Exception(tt.Texto.ToString());
}
// PUT: api/Teste/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE: api/Teste/5
public void Delete(int id)
{
}
}
}
This exception is giving 'Value can not be null'. The other test informing the type of the 'Test' parameter is also getting null. In this case, the exception that is thrown is null object reference.
Follow JSON from POSTMAN:
{
"Teste":{
"texto":"teste de post para controller"
}
}
Already test GET and it works normally.
Can anyone give me a hand with this? How do I get the object correctly?