0
I have a controller with a POST method that always receives the null parameter. I’m sending JSON through Portman.
I have tried to put the class as parameter, I have tried for string as parameter and in both cases is leaving it null;
Follows 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 cannot be null'. The other test stating the type of the 'Test' parameter is also receiving null. In this case, the Exception that is launched is of null object reference.
Follows JSON sent by POSTMAN:
{
"Teste":{
"texto":"teste de post para controller"
}
}
Already test the GET and it works normally.
Can someone give me a hand with this? How do I get the object correctly?
Where’s the code from where you’re doing the post?
– Leandro Angelo
From what I understand of your question you want to know where the POST is coming from. I am sending by POSTMAN. I go to headers put the Content-type in application/json and describe the file in the Body tab and give 'send'
– ABCDEFGH
This is in Asp.net core?
– Amadeu Antunes