Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve a namespace error in BlazorServer default project solutions (VisualStudio code)

I've created multiple new BlazorServer projects in vs code using dotnet new blazorserver on multiple pcs and I always get this error.

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

Compiles and runs with no problems but its quite annoying to have a constant error showing.

Has anyone else been able to resolve this issue?

Example

like image 923
JamesC Avatar asked Oct 29 '25 15:10

JamesC


1 Answers

The answer is that the namespace is "inferred" (best word I can think of), i.e. there are Razor files in the Shared folder without a @namespace entry. When the Razor compiler builds them into classes the correct namespace information is added. Visual Studio understands this, but Visual Studio Code obviously doesn't.

The simple fix is to add a @namespace xxxxxx.Shared entry to one of the Razor files in Shared. VSC then see's the namespace directly and the error message goes away. It's the same for any directory with Razor files and inferred namespaces.

like image 143
MrC aka Shaun Curtis Avatar answered Oct 31 '25 06:10

MrC aka Shaun Curtis