Blazor the type or namespace name 'App' could not be found (are you missing a using directive or an assembly reference?)

Viewed 16702

I have setup the basic application in Blazor in Microsoft Visual Studio Community 2019 Version 16.1.3 and I have tried both of the versions of .NET Core SDK 3.0.100-preview5-011568 and SDK 3.0.100-preview6-012264.

Also I have installed the Blazor extension but when I am building without any modification in any files, the build fails with the following error:

"The type or namespace "App" could not be found.

Any help will be highly appreciated. Thanks.

13 Answers

To use Blazor you need to have VS2019 preview edition or enable preview features on VS2019 by checking

Tools -> Options -> Environment -> Preview Features -> Use previews of the .NET Core SDK

or on the older versions of VS2019

Tools -> Options -> Projects and Solutions -> .NET Core -> Use previews of the .NET Core SDK

checkbox, then reload the solution and build.

<component type="typeof(App)" render-mode="ServerPrerendered" />

The error I got (using Visual Studio 2019 v16.8.2 & .NET 5 in a Blazor Server App) was pointing to the "App" part of the line above (in the file _Host.cshtml). This happened after copying in some code from another project, and encountering issues with a different (application) namespace, I got this error (and no others) when trying to build. @SᴇM's answer helped me solve the problem - I closed and re-opened my solution, and then got namespace errors when building - which I could resolve... after fixing that it worked.

I would have commented to @SᴇM's solution, but I don't have enough 'reputation'.

For me, Blazor app was building locally, but not in Azure pipeline.

Adding App.razor.cs solved the build problem.

using Microsoft.AspNetCore.Components;

namespace BlazorDemo.Client
{
    public partial class App : ComponentBase
    {
    }
}

Addition to @SᴇM answer, please make sure you are using latest version of Visual studio. For my case, I followed the same steps mentioned by @SᴇM but still option Blazor App option not shown.

After that, I updated visual studio to 16.3.7 and now option is visible while creating new project. :)

In my case the problem was that I had the following package reference in my csproj file:

<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />

Removing this fixed the issue.

The following output in my build.log file helped me track this down

1>/usr/local/share/dotnet/sdk/3.1.402/Sdks/Microsoft.NET.Sdk.Razor/build/netstandard2.0/Sdk.Razor.CurrentVersion.targets(404,5): warning RAZORSDK1006: Detected Razor language version downgrade. This is typically caused by a reference to the Microsoft.AspNetCore.Razor.Design package. Consider removing this package reference.

After migrating my .NET Core 3.1 application to .NET 5.0 I experienced the same problem. My application was working just fine but since I opened my App.razor file errors showed.

"the type or namespace name 'pageName' could not be found" and the pages show normally.

Fixed the problem with Tools -> Options -> Environment -> Preview Features -> Use previews of the .NET Core SDK. Restart of Visual Studio and reloading project was also needed.

Which is kind of weird since I just moved to .NET 5.0

Restart Visual Studio worked for me

In my case the issue was the namespace definition in _Host.cshtml. Renaming it to the correct namespace solved the issue.

For me adding the namespace with a using statement worked.

@using PieShopHrmClientWebAssembly.App.Shared

Here PieShopHrmClientWebAssembly.App is my Blazor project name.

In addition to @Jonno answer, if you copy razor files you need to change Build Action to Content in File Properties

Renaming my project from "App" to "GCL-App" worked for me, as it is throwing "circular dependency issue inclusive of the above error.

I had a similar error, and restarting Visual Studio 2019 fixed it for me.

This seems to be a generic message that the compilation has failed. In my case the Error List was not useful, but Build Output had more information (search for "error") but it only had a partial hint:

warning CS8785: Generator 'RazorSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'KeyNotFoundException'

It was still not enough, so I right-clicked the solution and chose "open in terminal" and there I executed this command manually:

dotnet build

And this one gave me some more information about the actual key not being found ("TableItem" in my case). This led me to some trial and error and I had some conflicting libraries (custom copy of BlazorTable and a new nuget version of MudBlazor) with similar component names.

I renamed <Table> to <BlazorTable>, and later to <BlaTable> 
(and ended up using <BlazorTable.BlaTable> to avoid repeating the namespace in class name)

Since I was still getting errors, I also removed all "@using BlazorTable" and instead included namespace in all tags, including attribute values in enum cases:

<BlazorTable.BlaTable TableItem="BillPlanListModel"
    Items=Rows 
    SelectionType=BlazorTable.SelectionType.Single>
        <BlazorTable.Column TableItem="BillPlanListModel" />
</BlazorTable.BlaTable>

This was a bit painful but it helped in my case after a few hours of work, and now I could upgrade MudBlazor to a new version. I was using .net 6 in VS2022, and got this error when updating MudBlazor from 6.02 to 6.03 (and later I was able to update MudBlazor to 6.0.10).

Related