Jam*_*esC 4 c# namespaces visual-studio-code blazor blazor-server-side
我dotnet new blazorserver在多台电脑上使用 vs code 创建了多个新的 BlazorServer 项目,但总是收到此错误。
The type or namespace name 'Shared' does not exist in the namespace 'BlazorServerTutorial' (are you missing an assembly reference?) [BlazorServerTutorial]
Run Code Online (Sandbox Code Playgroud)
编译和运行没有任何问题,但不断显示错误是很烦人的。
还有其他人能够解决这个问题吗?
答案是命名空间是“推断的”(我能想到的最好的词),即共享文件夹中存在没有@namespace条目的 Razor 文件。当 Razor 编译器将它们构建到类中时,会添加正确的命名空间信息。Visual Studio 理解这一点,但 Visual Studio Code 显然不理解。
简单的修复方法是向Shared@namespace xxxxxx.Shared中的 Razor 文件之一添加一个条目。然后,VSC 会直接看到命名空间,错误消息就会消失。对于任何具有 Razor 文件和推断命名空间的目录都是一样的。