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

Blazor项目CS1061错误:Report对象无Title属性定义求助

解决Blazor中Report.razor的CS1061错误

以下是针对你遇到的CS1061(无法解析Report对象属性)问题的排查和解决步骤:

1. 修正API请求路径

你的客户端请求路径完全写反了:

  • 服务器端ReportController的路由是[Route("[controller]")],对应路径前缀为/Report
  • List Action的路由是[HttpGet("List")],完整请求路径应为/Report/List

修正客户端OnInitializedAsync中的代码:

protected override async Task OnInitializedAsync()
{
    Reports = await Http.GetFromJsonAsync<List<Report>>("Report/List") ?? new();
}

2. 确认Client项目已引用Shared项目

如果Client项目没有正确引用Shared项目,IDE无法识别Report类的属性定义:

  • 在解决方案资源管理器中,右键Client项目 → 选择"添加" → "项目引用"
  • 勾选Shared项目,点击确定

3. 清理并重建解决方案

IDE编译缓存可能导致错误残留:

  • 右键解决方案 → 选择"清理解决方案"
  • 右键解决方案 → 选择"生成解决方案"
  • 重启IDE(可选,彻底清除缓存)

4. 完善空值安全处理

GetFromJsonAsync可能返回null,添加空合并运算符确保Reports始终不为空,同时帮助IDE正确推断类型:

private List<Report> Reports = new();

protected override async Task OnInitializedAsync()
{
    Reports = await Http.GetFromJsonAsync<List<Report>>("Report/List") ?? new();
}

修正后的完整客户端代码(Report.razor)

@page "/reports"
@using BlazorApp.Shared
@using Microsoft.AspNetCore.Authorization;
@attribute [Authorize]
@inject HttpClient Http

<PageTitle>Report</PageTitle>

<h1>Report</h1>

<ul>
    @foreach (var report in Reports)
    {
        <li>@report.Title</li>
    }
</ul>

@code {
    private List<Report> Reports = new();

    protected override async Task OnInitializedAsync()
    {
        Reports = await Http.GetFromJsonAsync<List<Report>>("Report/List") ?? new();
    }
}

内容的提问来源于stack exchange,提问作者Elia Krummenacher

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 07:53:08