尽管索引存在,但找不到视图“索引” - ASP.NET Core 6 MVC

coo*_*ryu 8 asp.net-core-mvc asp.net-core .net-6.0

我试图在本地主机上运行我的 asp.net 应用程序,但似乎出现以下错误:

处理请求时发生未处理的异常。

InvalidOperationException:未找到视图“索引”。搜索了以下位置:
/Views/Web/Index.cshtml
/Views/Shared/Index.cshtml
/Pages/Shared/Index.cshtml

我的观点位于Views > Web > Index.cshtml,尽管我仍然没有找到解决此问题的方法。我已经阅读了 asp.net 文档和其他 stackoverflow 帖子。似乎无法解决问题。

使用:

  • 视觉工作室 2019
  • ASP.NET 核心 6
  • Windows 10

这是我的program.cs文件中的代码

    using UploadExcel.Context;
    using UploadExcel.Service;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container.
    builder.Services.AddRazorPages();
    builder.Services.AddDbContext<DatabaseContext>();
    builder.Services.AddScoped<IWebService, WebService>();
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    
    app.UseRouting();
    
    app.UseAuthorization();
    
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Web}/{action=Index}/{id?}");
    });
    
    app.MapRazorPages();
    
    app.Run();
Run Code Online (Sandbox Code Playgroud)

小智 12

此问题的原因之一可能是 cshtml 文件的构建操作。

  • 转到存在此问题的 cshtml 文件。
  • 右键单击它并更改属性[或单击 Alt + Enter ]
  • 将名为“构建操作”的属性从“无”更改为“内容”

如下图所示:

没有选择任何一个

打开DDL

选择内容


Nis*_*nga 11

如果您在Visual Studio 2022、ASP.Net Core MVC ( .NET 6 )上遇到此问题,作为立即解决方案,您可以打开*.csproj文件所在的终端并执行,

dotnet watch run
Run Code Online (Sandbox Code Playgroud)

如果您确实想使用 Visual Studio,则必须将 Visual Studio 2022 更新到版本 17.1.0 +(您当前的版本可能是 17.0.4 或其他版本)。参考这个这个


Ser*_*rge 0

因为您正在使用控制器,所以您必须添加

builder.Services.AddControllersWithViews();
Run Code Online (Sandbox Code Playgroud)