Home > OS >  Adding Global Using Directive in blazor
Adding Global Using Directive in blazor

Time:05-16

The type or namespace name 'XXX' does not exist in the namespace 'MyClassLibrary.Models' (are you missing an assembly reference?). The error refer to this BlazorProject\MyPager.azor.g.cs

I got this error after this scenario:

  1. Created a file of type .cs inside my "Blazor project" and I added all my global directives by using keyword global inside this file.
  2. Changed some namespaces in my "Class library"

Structure of the project:

  1. Blazor-serve-side project using the class library
  2. Class library.

When I go the page , I can't see any error, the error just in VS, I tried to clean the solution, rebuild, closing VS and reopen but it doesn't solve the problem. I am using VS22 and .Net6.

Note I have also in my blazor project _Imports.razor file

CodePudding user response:

@MisterMagoo, Thanks a lot. what you said works fine, plus that I have to repeat the same namespaces with global directive file and also in _Imports.razor file, so the soultion:

  • In GlobalUsings.cs:

    global using Microsoft.AspNetCore.Components;
    global using Microsoft.AspNetCore.Components.Forms;
    
  • In _Imports.razor if you need the same namespaces in your razor pages, then you have to repeat them.

    using Microsoft.AspNetCore.Components;
    using Microsoft.AspNetCore.Components.Forms;
    
  • Related