'Application Parts (Controllers, Views, Tag Helpers, RazorPages) from referenced assemblies compile with errors after migrating to NET 6

I have an asp.net core project built starting from net core 3.1 into which I required to separate some controllers and views into assemblies, so I used application parts technique. I followed as indicated in this article and this other article to achieve that, so I started by configuring csprojs related with those dlls:

<Project Sdk="Microsoft.NET.Sdk.Razor">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <EnableDefaultContentItems>false</EnableDefaultContentItems>
    <AddRazorSupportForMvc>true</AddRazorSupportForMvc>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>
  
  <ItemGroup>
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
    <PackageReference Include="Telerik.UI.for.AspNet.Core" Version="2021.3.1207" />
  </ItemGroup>
  
  <ItemGroup>
    <Content Include="Views\Shared\_ComponenteDivisionPoliticaPartial.cshtml">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
    <Content Include="Views\Shared\_ComponenteOrganismoTransitoPartial.cshtml">
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content> 
    <Content Include="Views\Shared\_ComponenteAgregarPersonaPartial.cshtml">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
    <Content Include="Views\Shared\_ComponentePersonasPartial.cshtml">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
    <Content Include="Views\Shared\_PageScriptsPartial.cshtml">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
  </ItemGroup>
  
  <ItemGroup>
    <Folder Include="Resources\Controllers\" />
    <Folder Include="Resources\Models\" />
  </ItemGroup>

</Project>

Then, into each dll project I added _ViewImports.cshtml to share imports and enable taghelpers and other things, as shown below:

enter image description here

Lastly, I configured the Startup class to add application parts:

enter image description here

After solving some issues, I achieved to run those views successfully from the main application. However I had to migrate the framework from 3.1 to NET 6, I solved different errors but for some reason, application parts' assemblies reported different errors and all related with usings, injects and others when compiling all Views/Shared, because the content of the _ViewImports files weren't recognized or loaded by default in Visual Studio 2022. As you know this ViewImport file is to configure imports, injects, etc, as a shared resource for all views.

As a workaround, I had to copy the content of the view imports and paste it into each View file to get each project compiled successfully, as shown below in a cshtml View:

enter image description here

I appreciate any help.



Solution 1:[1]

I solved the issue by removing EnableDefaultContentItems entry from PropertyGroup in all csproj related with razor views.

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>

This link is related and explain about it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aldemar Cuartas Carvajal