Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC can't find Views after converting to ASPNET Core 3.1

We are converting an MVC/Angular web site from ASPNET Core 2.2 to ASPNET Core 3.1. I have got the build to run with no errors or warnings, but when it starts up (in Visual Studio 2019), it looks for Home/Index, finds the controller fine, and looks for the Home/Index View, but it fails to find it even though it is there (in Views/Home/Index.cshtml), right where it's supposed to be (and right where it's been for the several months that we have been developing this site. I have it now reduced to a test case that I can set the web project file to netcoreapp2.2 and the site runs fine, then stop it and change the to netcoreapp3.1, with no other changes, and it fails. The errors are exactly what you would expect if the file were missing:

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

/Views/Home/Index.cshtml

/Views/Shared/Index.cshtml

/Pages/Shared/Index.cshtml

But the file is there! I'm completely baffled.

Can anyone give me a hint as to what's going on?

Thanks.

like image 412
Dave Hanna Avatar asked Dec 05 '25 15:12

Dave Hanna


1 Answers

We just had a similar issue with partials: after upgrading a web app from core 2.2 to 3.1 partial views could not be found. The solution for us was to replace this...

services.AddMvc();

... with ...

services.AddControllersWithViews();
services.AddRazorPages().AddRazorRuntimeCompilation();

Although I believe .AddMvc() is equivalent to .AddControllersWithViews() plus .AddRazorPages it doesn't include the runtime compilation part out of the box.

like image 95
centralscru Avatar answered Dec 07 '25 03:12

centralscru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!