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

求助:Blazor代码出现Namespace not found错误的原因排查

Blazor "Namespace not found" 错误解决

复制教程中可正常运行的代码后,出现Namespace not found错误,相关代码如下:

@using AnimalShelterApp
@page "/animal"
<h3>Animal</h3>

<div class="card" style="width: 18rem;">
    <!-- This is the Image tag -->
    <img src="@CurrentAnimal.PictureURL" class="card-img-top" alt="...">
    <!-- This is the Body of the Card -->
    <div class="card-body">
        <h5 class="card-title">@CurrentAnimal.Name</h5>
        <p class="card-text">
            @if (CurrentAnimal.AnimalKind == "Dog")
            {
                @: I'm a @CurrentAnimal.Gender dog that is @CurrentAnimal.EstimatedAge years old.
            }else
            {
                @: I'm a @CurrentAnimal.Gender cat that is @CurrentAnimal.EstimatedAge years old.
            }
        </p>
        <a href="#" class="btn btn-primary">Submit</a>
    </div>
</div>

@code {
    public Animal CurrentAnimal { get; set; }
    protected override void OnInitialized()
    {
        CurrentAnimal = new Animal
        {
                Id = 1,
                Name = "Max",
                AnimalKind = "Dog",
                EstimatedAge = 1,
                PictureURL = "https://pixabay.com/images/id-2785074/",
                Gender = "Male",
            };
    }
}

我的解决方案截图:
my solution

排查与解决步骤:

  • 核对Animal类命名空间:确保定义Animal类的文件中,namespace声明为AnimalShelterApp,与页面引用的命名空间完全一致。
  • 检查项目引用:如果Animal类在独立项目中,确认当前Blazor项目已添加对该项目的引用。
  • 清理重建解决方案:右键解决方案→清理解决方案,再重新生成项目,解决编译缓存导致的识别问题。
  • 全局导入命名空间:在Blazor项目的_Imports.razor文件中添加@using AnimalShelterApp,无需在每个页面单独引用。
  • 重启开发工具:若以上步骤无效,重启IDE(如Visual Studio),清除工具缓存后重新编译。

内容的提问来源于stack exchange,提问作者Neil Meyer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 07:05:03