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

如何解决ASP.NET Core 3.1中‘未找到Index视图’的异常?

问题:添加Razor视图后出现“Index视图未找到”运行时异常

添加Razor视图前应用运行正常,添加后抛出异常:

System.InvalidOperationException: The view 'Index' was not found,系统已搜索/Views/App/Index.cshtml和/Views/Shared/Index.cshtml路径。

已完成的排查操作:

  • 确认Index视图位置正确
  • 已安装Razor相关NuGet包
  • 已将Index.cshtml的生成操作设置为Content
  • Startup类中已配置services.AddControllersWithViews().AddRazorRuntimeCompilation()
  • 默认路由已指向App/Index

相关代码如下:

AppController 代码

using Microsoft.AspNetCore.Mvc;

namespace DutchTreat.Controllers
{
    public class AppController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

Index.cshtml 代码

@{ 
    ViewBag.Title = "Home Page";
}
<!DOCTYPE html>
<html>
<head>
    <title>Dutch Treat: @ViewBag.Title</title>
    <link href="~/css/site.css" rel="stylesheet" />
</head>
<body>
    <h2>@ViewBag.Title</h2>
    <div class="product-info">
        <img src="~/img/VanGogh.jpg" />
        <h3>Mug</h3>
        <ul class="product-props">
            <li>Price: $9.99</li>
            <li>Artist: Van Gogh</li>
            <li>Title:</li>
            <li>Description: Self-portrait</li>
        </ul>
        <button id="buyButton">Buy</button>
    </div>

    <div id="theForm">
        <form>
            <label>Your Name:</label>
            <br />
            <input />
            <br />
            <label>Email:</label>
            <br />
            <input type="email" />
            <br />
            <label>Subject:</label>
            <br />
            <input />
            <br />
            <label>Message:</label>
            <br />
            <textarea rows="4"></textarea>
            <br />
            <input type="submit" value="Send Message" />
        </form>
    </div>

    <div>
        <div id="loginToggle">Login</div>
        <form class="popup-form">
            <div>
                <label>Username</label>
                <input />
            </div>
            <div>
                <label>Password</label>
                <input />
            </div>
            <div>
                <input type="checkbox" />
                <label>Remember me</label>
            </div>
            <div>
                <input type="submit" value="Login" />
            </div>
        </form>
    </div>

    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/js/index.js"></script>
</body>
</html>

Startup 代码

using DutchTreat.Controllers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DutchTreat
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews().AddRazorRuntimeCompilation();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseStaticFiles();
            app.UseRouting();
            app.UseEndpoints(cfg =>
                    {
                        cfg.MapControllerRoute("Default",
                                               "/{controller}/{action}/{id?}",
                                               new { controller = "App", action = "Index" });
                    });
        }
    }
}

解决建议
  1. 严格检查文件夹大小写与路径
    确认项目中/Views/App/目录名称与控制器名称完全匹配(区分大小写,尤其是Linux/macOS环境下),Index.cshtml必须放在该目录内。

  2. 清理编译缓存并重新生成

    • 执行命令:dotnet clean
    • 手动删除项目根目录下的bin和obj文件夹
    • 重新编译:dotnet build
      编译缓存可能导致视图无法被正确识别。
  3. 手动指定视图路径测试
    修改控制器代码,明确指定视图的完整路径:

    public IActionResult Index()
    {
        return View("~/Views/App/Index.cshtml");
    }
    

    如果此方式能正常运行,说明自动视图查找逻辑存在隐性配置问题。

  4. 验证项目文件配置
    打开.csproj文件,确认Index.cshtml的条目为Content类型:

    <ItemGroup>
      <Content Include="Views\App\Index.cshtml" />
    </ItemGroup>
    

    若存在None类型的条目,直接修改为Content。

  5. 核对Razor包版本
    执行dotnet list package命令,确认Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation包版本与ASP.NET Core主版本完全一致,版本不匹配会导致运行时编译失效。

  6. 检查启动环境配置
    确认启动时未设置特殊环境变量(如ASPNETCORE_ENVIRONMENT=Production)导致视图加载逻辑变更,可临时切换到Development环境测试。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 09:41:29