HTTP GET JSON异步请求在Blazor服务器中不起作用
目前,我正在学习Blazor服务器制作一个辅助项目。我只是按照教程制作了一个简单的API控制器,并使用它在Razor页面上显示数据库。控制器像HTTP GET JSON异步请求在Blazor服务器中不起作用,json,async-await,httprequest,blazor-server-side,Json,Async Await,Httprequest,Blazor Server Side,目前,我正在学习Blazor服务器制作一个辅助项目。我只是按照教程制作了一个简单的API控制器,并使用它在Razor页面上显示数据库。控制器像 [Route("api/[controller]")] [ApiController] public class EmployeeController: ControllerBase { private readonly IEmployeeRepository _emp;
[Route("api/[controller]")]
[ApiController]
public class EmployeeController: ControllerBase
{
private readonly IEmployeeRepository _emp;
public EmployeeController(IEmployeeRepository emp) {
_emp = emp;
}
[HttpGet]
public async Task<ActionResult> GetEmployees() {
return Ok(await _emp.GetEmployees());
}
[HttpGet("{id}")]
public async Task<ActionResult> GetEmployee(int employeeId) {
return Ok(await _emp.GetEmployee(employeeId));
}
[HttpPost]
public async Task<ActionResult> AddEmployee(Employee employee) {
return Ok(await _emp.AddEmployee(employee));
}
[HttpPut]
public async Task<ActionResult> UpdateEmployee(Employee employee) {
return Ok(await _emp.UpdateEmployee(employee));
}
[HttpDelete("{id}")]
public async Task<ActionResult> DeleteEmployee(int employeeId) {
return Ok(await _emp.DeleteEmployee(employeeId));
}
}
在EmployeeService.cs
中,实现如下
public class EmployeeService: IEmployeeService
{
private readonly HttpClient _httpClient;
public EmployeeService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<IEnumerable<Employee>> GetEmployees() {
return await _httpClient.GetJsonAsync<Employee[]>("api/employee");
}
public async Task<Employee> GetEmployee(int id)
{
return await _httpClient.GetJsonAsync<Employee>($"api/employee/{id}");
}
}
但是当我运行应用程序并转到/empList
时,会出现一些错误,如
An unhandled exception occurred while processing the request.
JsonReaderException: '<' is an invalid start of a value. LineNumber: 1 | BytePositionInLine: 0.
System.Text.Json.ThrowHelper.ThrowJsonReaderException(ref Utf8JsonReader json, ExceptionResource resource, byte nextByte, ReadOnlySpan<byte> bytes)
JsonException: '<' is an invalid start of a value. Path: $ | LineNumber: 1 | BytePositionInLine: 0.
System.Text.Json.ThrowHelper.ReThrowWithPath(ref ReadStack state, JsonReaderException ex)
处理请求时发生未处理的异常。
JsonReaderException:'您需要检查从API调用中看到的响应-您应该能够输入此url并获得返回的结果。另外,如果您在服务器模式下运行,您从何处获取HttpClient?
@page "/empList"
@inject Services.IEmployeeService Service
@using Models
<h3>Employee List</h3>
<div>
@if (employees == null) {
<strong>@employees is parsing...</strong>
}else {
<table class="table">
<thead>
<tr>
<th class="col">Id</th>
<th class="col">Name</th>
<th class="col">Gender</th>
<th class="col">DOB</th>
<th class="col">Department</th>
</tr>
</thead>
<tbody>
@foreach (var employee in employees) {
<tr>
<th>@employee.EmployeeId</th>
<th>@employee.EmployeeName</th>
<th>@employee.Gender</th>
<th>@employee.DateOfBirth</th>
<th>@employee.DepartmentId</th>
</tr>
}
</tbody>
</table>
}
</div>
@code {
private IEnumerable<Employee> employees;
protected override async Task OnInitializedAsync() {
employees = await Service.GetEmployees();
}
}
An unhandled exception occurred while processing the request.
JsonReaderException: '<' is an invalid start of a value. LineNumber: 1 | BytePositionInLine: 0.
System.Text.Json.ThrowHelper.ThrowJsonReaderException(ref Utf8JsonReader json, ExceptionResource resource, byte nextByte, ReadOnlySpan<byte> bytes)
JsonException: '<' is an invalid start of a value. Path: $ | LineNumber: 1 | BytePositionInLine: 0.
System.Text.Json.ThrowHelper.ReThrowWithPath(ref ReadStack state, JsonReaderException ex)
JsonReaderException: '<' is an invalid start of a value. LineNumber: 1 | BytePositionInLine: 0.
JsonException: '<' is an invalid start of a value. Path: $ | LineNumber: 1 | BytePositionInLine: 0.
blazorserver_efcore_crud.Services.EmployeeService.GetEmployees() in EmployeeService.cs
+
return await _httpClient.GetJsonAsync<Employee[]>("api/employee");
blazorserver_efcore_crud.Pages.EmployeeList.OnInitializedAsync() in EmployeeList.razor
+
employees = await Service.GetEmployees();