Aspnetcore: Namespaces dependent on solution folder name?

Created on 17 May 2018  路  15Comments  路  Source: dotnet/aspnetcore

  • Blazor 0.3.0
  • VS 2017 15.7.1

Created a new solution via PowerShell:
c:\projects\CoolNewProject\ dotnet new blazor

Created a new project in c:\projectsCoolNewProject called CoolNewProject.

Normally I like to have my .sln file a directory above my projects, i.e.
c:\projects\CoolNewProject\CoolNewProject.sln

Then throw all of my src in 'src'.
c:\projects\CoolNewProject\src\CoolNewProject.csproj and all of my other project files.

The second I move everything into the 'src' folder from my newly created Blazor project I can no longer compile:

Error | CS0246 | The type or namespace name 'App' could not be found (are you missing a using directive or an assembly reference?) | CoolNewProject | C:\projects\CoolNewProject\src\Program.cs | 17 | Active

Additionally, none of the Pages can find their namespaces either, i.e.

Error | CS0234 | The type or namespace name 'Shared' does not exist in the namespace 'CoolNewProject' (are you missing an assembly reference?) | CoolNewProject | C:\Projects\CoolNewProject\src\obj\Debug\netstandard2.0\Pages\Counter.g.i.cs | 21 | Active
Done area-blazor bug

All 15 comments

I created a new CoolNewProject (Blazor hosted) in Visual Studio and closed VS. I moved all three projects into src subfolder and manually changed projects' paths in CoolNewProject.sln. Then I loaded solution in VS, compiled and everything works perfectly.

I repeated the above steps in console window and everything works as expected:

  • dotnet new blazorhosted
  • create src subfolder and move all three projects there
  • manually edit paths in solution file
  • dotnet build (in main CoolNewProject folder)
  • change directory to srcCoolNewProject.Server
  • dotnet run
  • start browser and connect to localhost:5000

I agree dotnew new blazorhosted works as expected

dotnet new blazer does not

I can confirm your observation - it is impossible to compile project created with dotnet new blazor after moving to src subfolder.

Originally I tested blazorhosted because you mentioned about sln file and blazor project is created without solution file. I don't know why, but it is probably somehow related to the fact that we have or we don't have a solution file.

Update, just tried this in 0.4.0 release, issue remains.

I can reproduce this issue as well.

Same issue with the folder-names located in the .sln-File.
They seem to generate the namespaces of the .g.i.cs - files

Reproduction-Steps:

1) Create new Blazor project "BlazorNamespaceTest" from VS-Template (using standalone "Blazor"-Template)
2) Edit the corresponding BlazorNamespaceTest.sln-File - change the Project-Reference (e.g. incorrect casing)
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorNamespaceTest", "BlazorNameSPACETest\BlazorNamespaceTest.csproj", "{B6D9B760-70C0-4361-B78A-4C60CB1332FD}"
3) Building the project will fail
4) The generated app.g.i.cs contains the wrong namespace "BlazorNameSPACETest"

Generated namespace should depend on *.csproj filename or RootNamespace property defined in *.csproj.

Yeah, just ran into this while trying Blazor the first time. I like to keep all my project folders in lowercase, so I ran dotnet new blazor -n MyCoolProject -o mycoolproject, which lead to an app that wouldn't compile. Renaming the folder to match the project name's casing fixed the build.

I think previous suggestions put it best, where the generated classes should load the name based on the csproj or RootNamespace of the project, that leads to the most consistent and intuitive behaviour imo

Yeah this a limitation we have right now unfortunately. I think we all agree that using RootNamespace would be the correct thing to do. This will require changes to the Razor SDK and VS to support, so its a few months away.

This has caused a ton of confusion and seems highly illogical. Good that it's going to be fixed. Just curious how it could have happened?

Just want to update (not surprised ... just say'in) that it's still happening (3.0 Preview 3 SDK and VS2019). I changed the solution folder name of the Blazor doc's sample app from BlazorSample to Blazor ... :boom: with the errors reported on this issue ... changing it back to BlazorSample and all is :+1: again.

This was actually fixed in VS 16.1 preview 1 - will be released "soon"

I've run into a related problem. I'm not trying to move projects around but I would like to use a different namespace.

When I change the namespace of a shared project class, the C# code and use it but razor pages cannot find it.

I want this because I'm sharing Entities between a blazor project and Angular and I don't want to have a shared project namespace of "bm2portal_blazor.Shared", which is the name of the project. I want to namespace for my entities to be "Shared.Entities".

Will this be fixed in Blazor to work as it does in other C# code?

Thanks,
Bob

Check if you have @namespace bm2portal_blazor.Shared somewhere.

The order that a namespace is resolved for generated code (in priority order):

  • @namespace
  • <RootNamespace? in the project file
  • Project name

You can use either of those things above the project name to configure this, but my guess is that you have an @namespace somewhere that's causing your issue.

I've also filed this issue to track updating the docs https://github.com/aspnet/AspNetCore.Docs/issues/14685

Was this page helpful?
0 / 5 - 0 ratings