简体   繁体   English

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

[英]Cannot find view 'Index', although index is there - ASP.NET Core 6 MVC

I am trying to run my asp.net application on localhost, but seem to get the following error:我试图在本地主机上运行我的 asp.net 应用程序,但似乎出现以下错误:

An unhandled exception occurred while processing the request.处理请求时发生未处理的异常。

InvalidOperationException: The view 'Index' was not found. InvalidOperationException:未找到视图“索引”。 The following locations were searched:搜索了以下位置:
/Views/Web/Index.cshtml /Views/Web/Index.cshtml
/Views/Shared/Index.cshtml /Views/Shared/Index.cshtml
/Pages/Shared/Index.cshtml /Pages/Shared/Index.cshtml

I have my view located in Views > Web > Index.cshtml , although I still haven't found a way to resolve this issue.我的视图位于Views > Web > Index.cshtml中,但我仍然没有找到解决此问题的方法。 I have gone through the asp.net documentation and other stackoverflow posts.我浏览了 asp.net 文档和其他 stackoverflow 帖子。 Can't seem to solve the issue.似乎无法解决问题。

Using:使用:

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

Here is the code in my program.cs file这是我的 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();

If you are experiencing this issue on Visual Studio 2022 , ASP.Net Core MVC ( .NET 6 ), as an immediate solution what you can do is open a terminal where your *.csproj file resides and execute,如果您在Visual Studio 2022 、 ASP.Net Core MVC ( .NET 6 ) 上遇到此问题,作为直接解决方案,您可以打开*.csproj文件所在的终端并执行,

dotnet watch run

If you really want to use Visual Studio then you must update Visual Studio 2022 to version 17.1.0 + (your current version might be 17.0.4 or something).如果您真的想使用 Visual Studio,那么您必须将 Visual Studio 2022 更新到版本 17.1.0 + (您当前的版本可能是 17.0.4 或其他版本)。 Refer this and this .参考这个这个

since you are using controllers you have to add由于您使用的是控制器,因此您必须添加

builder.Services.AddControllersWithViews();

May be your applicaiton is opening on VS2019, please open the project in VS2022, issue will be fixed.可能是你的应用程序是在VS2019上打开的,请在VS2022上打开项目,问题将得到解决。

One of the causes of this problem can be the build action of the cshtml file.此问题的原因之一可能是 cshtml 文件的构建操作。

  • Go to the cshtml file that have this issue. Go 到有此问题的 cshtml 文件。
  • Right click it and change properties [ or click Alt + Enter ]右键单击它并更改属性 [或单击 Alt + Enter ]
  • Change the property named Build Action from None to Content将名为Build Action的属性从 None 更改为Content

As shown in the images below:如下图所示:

None is selected没有选择

Open DDL打开 DDL

Select Content Select 内容

  1. install Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation from NuGet Package Manager.从 NuGet Package 管理器安装 Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation。

Add below lines in program.cs在 program.cs 中添加以下行

  1. builder.Services.AddMvc().AddRazorRuntimeCompilation();
  2. builder.Services.AddControllersWithViews();
  3. app.MapControllers();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM