You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core MVC项目找不到Index.cshtml的InvalidOperationException错误求助

解决ASP.NET Core MVC中视图'Index'找不到的问题

下面是针对该错误的排查和解决步骤:

  • 检查视图文件属性
    右键项目中的Index.cshtml文件,查看「属性」面板里的「复制到输出目录」选项,必须设置为「如果较新则复制」或「始终复制」。若设置为「不复制」,编译后输出目录中不会包含该文件,运行时自然无法找到。

  • 验证控制器返回逻辑
    检查HomeController中返回视图的代码,确保没有错误指定视图名称(ASP.NET Core默认区分大小写):

    public IActionResult Index()
    {
        // 直接return View()即可,不要错误写成return View("index")
        return View();
    }
    
  • 确认视图文件路径
    检查磁盘上的实际文件结构,确保Index.cshtml确实位于/Views/Home/目录下,避免出现目录拼写错误(比如把Home写成home或Homes)。Visual Studio解决方案资源管理器的显示结构可能和实际磁盘目录不一致,需直接核对磁盘路径。

  • 清理重建项目
    执行「清理解决方案」→「重建解决方案」,或者手动删除项目的bin和obj目录后重新编译。编译缓存可能导致视图文件未正确输出。

  • 检查视图引擎配置
    若项目自定义了视图引擎路径,在Program.cs(.NET 6+)或Startup.cs(.NET 5及以前)中确认视图搜索路径配置正确,不要误删默认路径:

    // .NET 6+ 示例配置
    builder.Services.AddControllersWithViews()
        .AddRazorOptions(options =>
        {
            // 保留默认的视图搜索路径
            options.ViewLocationFormats.Add("/Views/{1}/{0}.cshtml");
            options.ViewLocationFormats.Add("/Views/Shared/{0}.cshtml");
        });
    
  • 排查区域(Area)配置
    若项目使用了区域,确保控制器上添加了[Area("AreaName")]特性,视图文件放在/Areas/AreaName/Views/Home/Index.cshtml路径下,同时路由配置正确。


错误详情

未处理的异常在处理请求时发生。
InvalidOperationException: 未找到视图'Index'。已搜索以下位置:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable originalLocations)
Stack Query Cookies Headers Routing
InvalidOperationException: 未找到视图'Index'。已搜索以下位置:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable originalLocations)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|30_0<TFilter, TFilterAsync>(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

内容的提问来源于stack exchange,提问作者user22624857

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 01:00:34