View is not found when migrating from AspNet.Core 2.2 to 3.0

197 Views Asked by At

I have an Asp.Net Core 2.2 MVC application. I am trying to migrate to 3.0, using this documentation: https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-6.0&tabs=visual-studio.

So in Startup.cs I replaced

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

with

        app.UseRouting();
        ...
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(name: "Default", pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });

I am getting

InvalidOperationException: The view 'Index' was not found. The following locations were searched:

/Views/Home/Index.cshtml

/Views/Shared/Index.cshtml

/Pages/Shared/Index.cshtml

What am I missing?

ADDED: Here is my project file:

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

<PropertyGroup>
  <TargetFramework>netcoreapp3.0</TargetFramework>
  <UserSecretsId>HIDDEN</UserSecretsId>
</PropertyGroup>

<ItemGroup>
  <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
  <None Remove="ScaffoldingReadme.txt" />
</ItemGroup>

<ItemGroup>
  <PackageReference Include="bootstrap-select" Version="1.13.12" />
  <PackageReference Include="CsvHelper" Version="12.1.2" />
  <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.0.0" />
  <PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="3.0.0" />
  <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0" />
  <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
  <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0" />
  <PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.1.0" />
  <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="3.0.0" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="3.0.0" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.0.0" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Primitives" Version="3.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Razor" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
<PackageReference Include="Sendgrid" Version="9.11.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
<Content Update="Views\Allocation\Index.cshtml">
  <Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
</ItemGroup>
<ItemGroup>
  <None Update="appsettings.json">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </None>
</ItemGroup>
</Project>

enter image description here

1

There are 1 best solutions below

14
On

Follow your steps to migrate project from 2.2 to 3.0 version. It works fine in my side.

Then I rename the Index.cshtml -> Index1.cshtml, then I can reproduce the issue. So please check the Index.cshtml in your project, you also can click show all file button to check all the content.

enter image description here

Just make sure Index.cshtml file is not excluded from the project.