【问题标题】:How to resolve a namespace error in BlazorServer default project solutions (VisualStudio code)如何解决 Blazor Server 默认项目解决方案中的命名空间错误(Visual Studio 代码)
【发布时间】:2021-08-29 00:53:48
【问题描述】:

我在多台电脑上使用 dotnet new blazorserver 在 vs 代码中创建了多个新的 BlazorServer 项目,但我总是收到此错误。

The type or namespace name 'Shared' does not exist in the namespace 'BlazorServerTutorial' (are you missing an assembly reference?) [BlazorServerTutorial]

编译和运行都没有问题,但是总是显示错误很烦人。

还有其他人能够解决这个问题吗?

Example

【问题讨论】:

  • 是的,我一直都明白这一点。我认为 Intellisense 跟不上。我没有解决方案——人们已经说了很多关于如何“解决”这些问题的内容,但大多数情况下看起来像是“摇晃它并希望它有效。”
  • 碰巧你删除了WeatherForecast.cs。这将导致错误,因为命名空间中没有任何内容。

标签: c# visual-studio-code namespaces blazor blazor-server-side


【解决方案1】:

答案是命名空间是“推断的”(我能想到的最好的词),即 Shared 文件夹中有 Razor 文件没有 @namespace 条目。当 Razor 编译器将它们构建到类中时,会添加正确的命名空间信息。 Visual Studio 明白这一点,但 Visual Studio Code 显然不明白。

简单的解决方法是将@namespace xxxxxx.Shared 条目添加到共享 中的Razor 文件之一。 VSC 然后直接查看命名空间,错误消息消失。对于任何具有 Razor 文件和推断命名空间的目录来说都是一样的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多