You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor项目API调用异常求助:WebAssemblyRenderer报错排查

Blazor API调用Bug排查求助

我正在学习Blazor课程,现在遇到一个解决不了的Bug:我写了一个可通过API调用的UnitController,但无法成功调用该API。用Postman测试时返回“An unhandled error has occurred.”错误,而非预期的游戏单位列表。我推测可能是某个隐藏Bug导致列表无法被调用。作为首次接触该技术的新手,我没有.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

我怀疑问题和IUnitService接口里的Task LoadUnitsAsync();定义有关,但Visual Studio未给出任何错误提示。同样的问题也出现在UnitService中,我附上了UnitController、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");
        //     }
        // }
    }
}

UnitController代码

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);
        // }
    }
}

内容的提问来源于stack exchange,提问作者max biocca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 13:50:30