I have this Error
The Json value could not be converted to System.Collections.Genereic.List 1[Blazor_fresh_project.Shared.Feedback]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
when I'm trying to CreateObj in this method :
public async Task<List<Feedback>> CreateFeedback(Feedback feedback)
{
var resulte = await _httpClient.PostAsJsonAsync($"api/feedback",feedback);
string content = Newtonsoft.Json.JsonConvert.SerializeObject(resulte);
Console.WriteLine("Here is THE CONTENT " +content);
var feedbacks = await resulte.Content.ReadFromJsonAsync<List<Feedback>>();
return feedbacks;
}
This error happen at line :
var feedbacks = await resulte.Content.ReadFromJsonAsync<List<Feedback>>();
The console write :
Console debug
Do you have any suggestions or tips for debugging this kind of error?
Edit : api/feedback request
namespace Blazor_fresh_project.Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FeedbackController : ControllerBase
{
List<Feedback> Feedbacks = new List<Feedback> {
new Feedback{Id=1,Name="arthur",Description="I think this is great",DateOfPost=DateTime.Now,Power=10},
new Feedback{Id=2,Name="Mario",Description=" coming soon",DateOfPost=DateTime.Now,Power=8},
new Feedback{Id=3,Name="Chipolo",Description="I think this is great",DateOfPost=DateTime.Now,Power=17},
new Feedback{Id=4,Name="Coco",Description=" coming soon",DateOfPost=DateTime.Now,Power=12},
new Feedback{Id=5,Name="Marguerite",Description="I think this is great",DateOfPost=DateTime.Now,Power=6},
new Feedback{Id=6,Name="Carine",Description=" coming soon",DateOfPost=DateTime.Now,Power=4}
};
[HttpGet]
public async Task<IActionResult> GetFeedbacks()
{
return Ok(Feedbacks);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetSingleFeedback(int id)
{
var feedback = Feedbacks.FirstOrDefault(f => f.Id == id);
if (feedback == null)
return NotFound("this feedback wasn't found");
return Ok(feedback);
}
[HttpPost]
public async Task<IActionResult> CreateFeedback(Feedback feedback)
{
feedback.Id = Feedbacks.Max(h => h.Id + 1);
Feedbacks.Add(feedback);
return Ok(feedback);
}
}
}
namespace Blazor_fresh_project.Client.Services
{
public class FeedbackService : IFeedbackService
{
private HttpClient _httpClient;
public FeedbackService(HttpClient httpClient)
{
_httpClient = httpClient;
}
[Authorize]
public async Task<List<Feedback>> GetFeedbacks()
{
return await _httpClient.GetFromJsonAsync<List<Feedback>>("api/feedback");
}
public async Task<Feedback> GetFeedback(int id)
{
return await _httpClient.GetFromJsonAsync<Feedback>($"api/feedback/{id}");
}
[Authorize]
public async Task<List<Feedback>> CreateFeedback(Feedback feedback)
{
var resulte = await _httpClient.PostAsJsonAsync($"api/feedback",feedback);
string content = Newtonsoft.Json.JsonConvert.SerializeObject(resulte);
Console.WriteLine("Here is THE CONTENT " +content);
var feedbacks = await resulte.Content.ReadFromJsonAsync<List<Feedback>>();
return feedbacks;
}
}
}
[], the other not. You are not getting the same API results.