How do I solve the 'View not found' error in ASP.NET Core MVC in areas?

9.4k views Asked by At

I don't know why I get this error every time in ASP.NET Core MVC 3.1. I have a view and it's in its place. I only get this error in areas.

 InvalidOperationException: The view 'Dashboard' was not found. The following locations were searched:
    /Areas/Admin/Views/Home/Dashboard.cshtml
    /Areas/Admin/Views/Shared/Dashboard.cshtml
    /Views/Shared/Dashboard.cshtml
3

There are 3 answers

0
Nima Owji On BEST ANSWER

I resolved this problem by changing the area name from "AdminArea" to "Admin"

4
Jeremy Caney On

Nine times out of ten, this error on a view is because you forgot to put the [Area("Admin")] attribute at the top of your area controller(s).

Note: As you noted in your answer, this should not include the suffix Area.

While your area views are, by convention, searched for in /Areas/{Area}/Views/, there’s no effort to implicitly determine that a controller is intended for an area. In other words, .NET is exclusively relying on the [Area()] attribute, independent of whether your controller(s) live in e.g. /Areas/{Area}/Controllers/. You’ll probably store them there anyway for organizational purposes, but .NET doesn’t care either way.

0
Mike Cheel On

I had this problem recently (after upgrading a project from .NET 5.0 to .NET 6.0) and found when I built the project locally and then deployed manually everything worked but when building and deploying via Azure Devops it failed.

After much head scratching, it turned out to be that the build definition was still set to use Visual Studio 2019 when it should have been using Visual Studio 2022 (because it can build 6.0 projects).