Search code examples
c#asp.net-corerazor

How do I fixed conflict namespace named "Shared" in asp.net core _imports.razor


  1. Class library: Shared
  2. Namespace: @using Shared.Extensions

Error: CS0234
The type or namespace name 'Extensions' does not exist in the namespace 'Web.Areas.User.Views.Shared' (are you missing an assembly reference?)
Error location: _Imports.razor


Solution

  • I've just encountered this same problem. It appears when the razor file is compiled and you have an expression somewhere on the page/component, it is generating 'using' statements and declaring a variable to hold the result of the expression, without checking if there are namespace conflicts. This could be solved in the generator by prefixing the namespace with global::, but I can't see that happening any time soon.

    My current setup:

    • Foo.sln
      • Client.csproj
      • Server.csproj
      • Shared.csproj

    You need to prefix all the projects with a name so it doesn't conflict with Razor's default names:

    • Foo.sln
      • Foo.Client.csproj
      • Foo.Server.csproj
      • Foo.Shared.csproj

    To be honest, it's probably a good idea to fully qualify the projects anyway.