dotnet build_error ai_generated true

InvalidOperationException: 未找到视图'Index'。已搜索以下位置:/Views/Home/Index.cshtml, /Views/Shared/Index.cshtml, /Pages/Home/Index.cshtml

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

其他格式: JSON · Markdown 中文 · English
90%修复率
82%置信度
1证据数
2023-01-25首次发现

版本兼容性

版本状态引入弃用备注
.NET 6.0 active
.NET 7.0 active
.NET 8.0 active
ASP.NET Core MVC 6.0+ active
Razor Pages 6.0+ active

根因分析

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

English

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

官方文档

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

解决方案

  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.

无效尝试

常见但无效的做法:

  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% 失败

    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% 失败

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