question

Albert-6487 avatar image
0 Votes"
Albert-6487 asked BrandoZhang-MSFT commented

Blazor namespace and @namespace

I have a Blazor component where markup and code are separated ( AppNav.razor and AppNav.razor.cs).

Do I have to put namespace in both files or .razor.cs is sufficient?

I have this question because Visual studio 2019 (16.8.3) Enterprise is inconsistent. Sometimes it allows me to use components without specifying @namespace in razor file ( I assume it is enough to have it in .razor.cs). But after restarting Visual studio it gives me error about missing component and it only can be fixed if I put @namespace in *.razor file also.

So what is the correct way ?

AppNav.razor

 <div class="appnav">
     <div class="tree navigation">
     </div>
 </div>
 @namespace MyApp.Shared.App @*duplicating here*@
 @code {
                
 }

AppNav.razor.cs

 using Microsoft.AspNetCore.Components;
    
    
 namespace MyApp.Shared.App
 {
     public partial class AppNav
     {
         [Inject]
         private NavigationManager _navigation { get; set; }
    
         public void NavigateTo(string route)
         {
              _navigation.NavigateTo(route);
         }
     }
 }

_Imports.razor

 ...
 @using MyApp.State
 @using MyApp.Shared.App
 ...


















dotnet-csharpdotnet-aspnet-core-blazor
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Albert-6487 I guess this issue may be related with VS load slowly. I suggest you could try to check the VS's process to ready and check if this issue is disappear or not. Like this 62461-976.png


0 Votes 0 ·
976.png (16.9 KiB)

0 Answers