Additional component is not compiled

254 Views Asked by At

I have Component Views and everything works fine in development but when I published I get the exception

"InvalidOperationException: The view 'Components/SomeComponentName/Default' was not found".

The thing is I have two previous Component Views that work fine both in development and when published. Later I added new Component Views so the problem is the previous ones get compiled when published while the new ones is not compiled to the ProjectName.Web.Views.dll but still works fine when debugging.

Project is running dotnet core 3.1

2

There are 2 best solutions below

0
On

Usually, this happens when you made some changes to the component file meta data such as the component name or the file name. In my case, I initially named it default.cshtml but then renamed it to Default.cshtml in order to match the convention. However, Windows platform's file name system doesn't care too much about capitalization and this confuses Visual Studio and somehow messes up the project file and the file properties.

This is an interesting error and particularly hard to debug because the view component works fine when running in Visual Studio's debug mode but fails only in the release version.

When you see the InvalidOperationException with Component/.../Default not found error on the web page, it means the component file is not compiled by the build action. Then, right click the Default.cshtml file involved within Visual Studio's solution explorer and open its Properties window, select the Build Action back to Content on the dropdown list. This would fix the file not found error. There is no need to edit the project file directly.

enter image description here

0
On

I found the problem: The views were added to the Remove ItemGroup in Web.csproj file, so I deleted those lines and then Included it in the other ItemGroup

<ItemGroup>
    <Content Remove="Views\Shared\Components\SomeComponent1\Default.cshtml" />
    <Content Remove="Views\Shared\Components\SomeComponent2\Default.cshtml" />
.
.
.
</ItemGroup>

<ItemGroup>
    <None Include="Views\Shared\Components\SomeComponent1\Default.cshtml" />
    <None Include="Views\Shared\Components\SomeComponent2\Default.cshtml" />
.
.
.
</ItemGroup>