我正在做一门Blazor课程,却发现自己被一个我无法修复的错误所困。
我刚刚开发了一个应该可以通过API调用的控制器,但是我似乎不能调用那个API。我用邮递员来测试它,并收到一条错误消息:“一个未处理的错误发生了。”
而不是一个单位的列表(这是一个游戏)。我相信这取决于一个不同的bug,它可能使列表不可调用,这是我第一次使用这种语言,而且我发现很难解决这个问题,或者至少很难对issue.Especially进行分类,因为我以前大约有过0次使用.net或Java的经验。代码创建一个web应用程序(打开我的浏览器)。在浏览器的控制台中,我收到了错误消息:
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Exception of type 'System.Exception' was thrown.
System.Exception: Exception of type 'System.Exception' was thrown.
at BlazorBattles.Client.Services.UnitService.LoadUnitsAsync() in C:\Users\XXX\source\repos\BlazorBattles\BlazorBattles\Client\Services\UnitService.cs:line 37
at BlazorBattles.Client.Pages.Build.OnInitializedAsync() in C:\Users\XXX\source\repos\BlazorBattles\BlazorBattles\Client\Pages\Build.razor:line 33
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
window.Module.s.printErr @ blazor.webassembly.js:1
Fe._internal.dotNetCriticalError @ blazor.webassembly.js:1
St @ blazor.webassembly.js:1
_mono_wasm_invoke_js_blazor @ dotnet.6.0.10.95x5mwwwq1.js:1
$func219 @ 00971d2a:0x1a492
$func167 @ 00971d2a:0xce60
$func166 @ 00971d2a:0xbd73
$func2815 @ 00971d2a:0xabebf
$func1619 @ 00971d2a:0x6fc80
$func1617 @ 00971d2a:0x6fbf2
$func970 @ 00971d2a:0x50643
$func219 @ 00971d2a:0x1a44b
$func167 @ 00971d2a:0xce60
$func166 @ 00971d2a:0xbd73
$func2815 @ 00971d2a:0xabebf
$func1619 @ 00971d2a:0x6fc80
$func1623 @ 00971d2a:0x702ed
$mono_wasm_invoke_method @ 00971d2a:0x969f
Module._mono_wasm_invoke_method @ dotnet.6.0.10.95x5mwwwq1.js:1
managed__Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_BeginInvokeDotNet @ managed__Microsoft_A…eginInvokeDotNet:19
beginInvokeDotNetFromJS @ blazor.webassembly.js:1
b @ blazor.webassembly.js:1
e.invokeMethodAsync @ blazor.webassembly.js:1
(anonym) @ blazor.webassembly.js:1
ve @ blazor.webassembly.js:1
we @ blazor.webassembly.js:1
(anonym) @ blazor.webassembly.js:1
(anonym) @ blazor.webassembly.js:1
onGlobalEvent您可以在https://github.com/maxbiocca/BlazorBattles/tree/master/BlazorBattles下的Github中找到我所有的代码
它会对"Task LoadUnitsAsync()“表达式感到不高兴,因为它是公共接口IUnitService的一部分。由于在Visual中没有收到任何错误消息,所以我不知道问题在哪里,也不知道哪里缺少引用。
这个问题也出现在UnitService中。我复制了下面这三个元素(UnitControler、IUnitService、Unitservice)的代码--问题也发生在我没有附加的另外两个“页面”中。
我被困在这三天以来,无法找到前进的方式。我很乐意你的帮助,谢谢!
IUnitService
```
using BlazorBattles.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BlazorBattles.Client.Services
{
public interface IUnitService
{
IList<Unit> Units { get; }
IList<UserUnit> MyUnits { get; set; }
void AddUnit(int unitId);
Task LoadUnitsAsync();
}
}
```UnitService
using BlazorBattles.Shared;
using System.Net.Http.Json;
using System.Linq;
namespace BlazorBattles.Client.Services
{
public class UnitService : IUnitService
{
// private readonly HttpClient _http;
//public UnitService(HttpClient http){
// _http = http;
//}
public IList<Unit> Units => new List<Unit>
{
new Unit() {Id=1, Title = "Knight", Attack=10, Defense=10, BananaCost=100},
new Unit() {Id=2, Title = "Archer", Attack=15, Defense=5, BananaCost=150},
new Unit() {Id=3, Title = "Mage", Attack=20, Defense=1, BananaCost=200}
};
public IList<UserUnit> MyUnits { get; set; } = new List<UserUnit>();
public void AddUnit(int unitId)
{
var unit = Units.First(unit => unit.Id == unitId);
MyUnits.Add(new UserUnit { UnitId = unit.Id, HitPoints = unit.HitPoints });
}
public Task LoadUnitsAsync()
{
throw new Exception();
}
//public async Task LoadUnitsAsync(){
//if (Units.Count == 0)
//{
// Units = await _http.GetFromJsonAsync<IList<Unit>>("api/Unit");
//}
//}
}
}UnitControler
using BlazorBattles.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace BlazorBattles.Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UnitController : ControllerBase
{
public IList<Unit> Units => new List<Unit>
{
new Unit() {Id=1, Title = "Knight", Attack=10, Defense=10, BananaCost=100},
new Unit() {Id=2, Title = "Archer", Attack=15, Defense=5, BananaCost=150},
new Unit() {Id=3, Title = "Mage", Attack=20, Defense=1, BananaCost=200}
};
// [HttpGet]
//public IActionResult GetUnits()
// {
// return Ok(Units);
// }
}
}发布于 2022-11-03 21:54:51
我刚看过你的密码了。在BlazorBattles/Client/Pages/Build.razor中,您正在第33行中调用LoadUnitsAsync();。
但是,这个函数在您的服务中是这样实现的:
public Task LoadUnitsAsync()
{
throw new Exception();
} https://stackoverflow.com/questions/74210390
复制相似问题