Blazor WASM:ReadFromJsonAsync <List<Object>> Post crud 时出错
Blazor WASM : error while ReadFromJsonAsync <List<Object>> Post crud
我有这个Error
The Json value could not be converted to
System.Collections.Genereic.List
1[Blazor_fresh_project.Shared.Feedback]. Path: $ | LineNumber: 0 |
BytePositionInLine: 1.
当我尝试使用此方法创建对象时 :
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;
}
此错误发生在第 :
行
var feedbacks = await resulte.Content.ReadFromJsonAsync<List<Feedback>>();
控制台写:
Console debug
对于调试此类错误,您有什么建议或技巧吗?
编辑: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;
}
}
}
'Console' 屏幕截图向我们展示了单个反馈项的有效负载,浏览器中的请求显示了反馈列表。
您的代码正在执行 POST,浏览器执行 GET。显然,您的端点具有不同的 return 类型,用于 GET 和 POST.
Post 如果您需要更多帮助,请提供控制器的(相关)代码。
好的,你确实做到了。你可以改变你的控制器或者看起来更合乎逻辑的东西,你的客户:
public async Task<Feedback> CreateFeedback(Feedback feedback)
{
var resulte = await _httpClient.PostAsJsonAsync($"api/feedback",feedback);
var feedback = await resulte.Content.ReadFromJsonAsync<Feedback>();
return feedback;
}
并相应地调整调用代码。
我有这个Error
The Json value could not be converted to System.Collections.Genereic.List 1[Blazor_fresh_project.Shared.Feedback]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
当我尝试使用此方法创建对象时 :
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;
}
此错误发生在第 :
行 var feedbacks = await resulte.Content.ReadFromJsonAsync<List<Feedback>>();
控制台写:
Console debug
对于调试此类错误,您有什么建议或技巧吗?
编辑: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;
}
}
}
'Console' 屏幕截图向我们展示了单个反馈项的有效负载,浏览器中的请求显示了反馈列表。
您的代码正在执行 POST,浏览器执行 GET。显然,您的端点具有不同的 return 类型,用于 GET 和 POST.
Post 如果您需要更多帮助,请提供控制器的(相关)代码。
好的,你确实做到了。你可以改变你的控制器或者看起来更合乎逻辑的东西,你的客户:
public async Task<Feedback> CreateFeedback(Feedback feedback)
{
var resulte = await _httpClient.PostAsJsonAsync($"api/feedback",feedback);
var feedback = await resulte.Content.ReadFromJsonAsync<Feedback>();
return feedback;
}
并相应地调整调用代码。