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
...