dotnet build_error ai_generated true

InvalidOperationException: The view 'Index' was not found. The following locations were searched: /Views/Home/Index.cshtml, /Views/Shared/Index.cshtml, /Pages/Home/Index.cshtml

ID: dotnet/razor-pages-view-engine-missing-file

Also available as: JSON · Markdown · 中文
90%Fix Rate
82%Confidence
1Evidence
2023-01-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
.NET 6.0 active
.NET 7.0 active
.NET 8.0 active
ASP.NET Core MVC 6.0+ active
Razor Pages 6.0+ active

Root Cause

The Razor view engine cannot locate the specified view file because the file is missing, the path is incorrect, or the view has not been compiled (e.g., due to a missing @page directive or incorrect folder structure).

generic

中文

Razor视图引擎无法找到指定的视图文件,因为文件缺失、路径不正确或视图未编译(例如由于缺少@page指令或文件夹结构错误)。

Official Documentation

https://learn.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-8.0

Workarounds

  1. 95% success Create the missing view file at the correct location: /Views/{ControllerName}/{ActionName}.cshtml (e.g., /Views/Home/Index.cshtml) with the required Razor content.
    Create the missing view file at the correct location: /Views/{ControllerName}/{ActionName}.cshtml (e.g., /Views/Home/Index.cshtml) with the required Razor content.
  2. 85% success If using Razor Pages, ensure the page is in /Pages folder and has an @page directive at the top, with a matching route.
    If using Razor Pages, ensure the page is in /Pages folder and has an @page directive at the top, with a matching route.

中文步骤

  1. Create the missing view file at the correct location: /Views/{ControllerName}/{ActionName}.cshtml (e.g., /Views/Home/Index.cshtml) with the required Razor content.
  2. If using Razor Pages, ensure the page is in /Pages folder and has an @page directive at the top, with a matching route.

Dead Ends

Common approaches that don't work:

  1. Adding a view file with the same name but in a different folder (e.g., /Views/Shared/Index.cshtml instead of /Views/Home/Index.cshtml) 50% fail

    The view engine searches specific locations based on controller name; a shared view may not be picked up if the controller expects a specific view.

  2. Setting the view location format in Startup.cs to include custom paths without ensuring the actual file exists 30% fail

    The custom path is searched, but if the file is still missing, the error persists.