- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试过挖掘和调试,但似乎无法弄清楚为什么 Http.Json.GetFromJsonAsync 无法转换。我得到的错误如下(删节)
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[Onero.Shared.StudentDTO]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
System.Text.Json.JsonException: The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[Onero.Shared.StudentDTO].
这是我的代码:
// In the blazor wasm client
public async Task<IEnumerable<StudentDTO>> GetStudentsAsync()
{
return await http.GetFromJsonAsync<IEnumerable<StudentDTO>>("api/Students");
}
// My controller
[HttpGet]
public async Task<ActionResult<IEnumerable<StudentDTO>>> GetStudents()
{
return await _context.Students
.Select(x => StudentToDTO(x))
.ToListAsync();
}
// My classes
public class StudentDTO
{
public long ID { get; set; }
public string Name { get; set; }
public ICollection<ExamScores> Results { get; set; }
}
public class Student
{
public long ID { get; set; }
public string Name { get; set; }
public ICollection<Guardian> Guardians { get; set; }
public ICollection<ExamScores> Results { get; set; }
}
public class ExamScores
{
public long ID{ get; set; }
public long StudentId { get; set; }
public Student Student { get; set; }
public Exam ExamType { get; set; }
public double Score { get; set; }
}
// Startup.cs ConfigureServices
services.AddDbContext<ResultsContext>(opt =>
opt.UseSqlite(Configuration.GetConnectionString("FirstPrototype")));
services.AddControllersWithViews()
.AddJsonOptions(
x => x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve
);
// Context
public class ResultsContext : DbContext
{
public ResultsContext(DbContextOptions<ResultsContext> options)
: base(options)
{
}
public DbSet<ExamScores> ResultsList { get; set; }
public DbSet<Student> Students { get; set; }
public DbSet<Guardian> Guardians { get; set; }
}
当我尝试记录 Controller 方法时,我从 _context 返回的实际上是
System.Collections.Generic.List`1[Onero.Shared.StudentDTO]
我不确定这是否是我设置数据库和迁移的问题?也许它与我的 ConfigureServices
中的 JsonSerializerOptions
有关?但如果确实如此,那么为什么我的 Controller 方法报告我正在取回对象列表,但我的客户端无法反序列化它?我很感激帮助/建议。谢谢
最佳答案
事实上,是的,它确实与 JsonSerializerOptions
有关。我必须对客户端中的 GetStudentsAsync
方法执行以下操作:
public async Task<IEnumerable<StudentDTO>> GetStudentsAsync()
{
var options = new JsonSerializerOptions()
{
ReferenceHandler = ReferenceHandler.Preserve,
PropertyNameCaseInsensitive = true
};
return await http.GetFromJsonAsync<IEnumerable<StudentDTO>>("api/Students", options);
}
我从 this 收集到的然后我突然想到,也许属性名称由于大小写不匹配,这就是为什么我将 PropertyNameCaseInsensitive
设置为 true。
关于c# - 托管 Blazor WASM GetFromJsonAsync : The JSON value could not be converted to System. Collections.Generic.IEnumerable`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68246021/
我在Blazor的页面上有两个类似的电话:。第一个调用运行正常,而第二个调用失败。。下面是我在检查页面时收到的错误消息的开头:未处理的异常:System.Text.Json.JsonException
我在Blazor的页面上有两个类似的电话:。第一个调用运行正常,而第二个调用失败。。下面是我在检查页面时收到的错误消息的开头:未处理的异常:System.Text.Json.JsonException
我在Blazor的页面上有两个类似的电话:。第一个调用运行正常,而第二个调用失败。。下面是我在检查页面时收到的错误消息的开头:未处理的异常:System.Text.Json.JsonException
我在Blazor的页面上有两个类似的电话:。第一个调用运行正常,而第二个调用失败。。下面是我在检查页面时收到的错误消息的开头:未处理的异常:System.Text.Json.JsonException
System.Net.Http.Json的HttpClient扩展方法(例如GetFromJsonAsync())极大地简化了从Web API检索json对象的例程代码。使用起来很愉快。 但是由于它的
我新建了一个 西装外套 应用程序,我想通过 访问 URL HttpClient 我遇到了这个错误 'HttpClient' does not contain a definition for 'Get
我在 Blazor 服务器项目的 Razor 组件页面 Index.razor 上收到消息“CS1929 - 'IHttpClientFactory' 不包含'GetFromJsonAsync'的定义
我尝试过挖掘和调试,但似乎无法弄清楚为什么 Http.Json.GetFromJsonAsync 无法转换。我得到的错误如下(删节) blazor.webassembly.js:1 crit: Mic
我正在从 blazor 3.2.0 客户端调用 webapi: protected override async Task OnParametersSetAsync() { if (Sele
我是一名优秀的程序员,十分优秀!