简体   繁体   English

ASP.NET Core 6 MVC - 未找到视图“索引”

[英]ASP.NET Core 6 MVC - The view 'Index' was not found

I can run my asp.net 6 mvc app in VS2022 with no issues.我可以在 VS2022 中毫无问题地运行我的 asp.net 6 mvc 应用程序。 When I publish to a folder and deploy to IIS or run the exe myself I get an error ' the view Index was not found '.当我发布到文件夹并部署到 IIS 或自己运行 exe 时,我收到错误消息“未找到视图索引”。

The search locations reported are correct.报告的搜索位置是正确的。 My views are in the correct folder.我的视图位于正确的文件夹中。

在此处输入图像描述

I have also added Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation nuget package and updated Program.cs:我还添加了 Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation nuget package 并更新了 Program.cs:

builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();

The *.cshtml files Build Action are set to Content in Visual Studio. *.cshtml 文件生成操作在 Visual Studio 中设置为内容。

Nothing suggested in various stackoverlow posts resolves the issue.各种 stackoverlow 帖子中的任何建议都无法解决问题。 The majority are related to a VS2022 issue requiring Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package to be added.大多数与 VS2022 问题相关,需要添加 Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package。

I've set stdoutLogEnabled="true" and verified the log generated when running through IIS matches the log output when calling the.exe.我已设置 stdoutLogEnabled="true" 并验证运行 IIS 时生成的日志与调用 .exe 时的日志 output 匹配。 I don't believe this is an IIS issue.我不认为这是 IIS 问题。

Below is my Program.cs:下面是我的 Program.cs:

     var builder = WebApplication.CreateBuilder(args);

    // Add services to the container.
    builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
    builder.Services.AddDistributedMemoryCache();
    builder.Services.AddHttpContextAccessor();

    var app = builder.Build();
    app.UseExceptionHandler("/Home/Error");
    app.UseStatusCodePagesWithReExecute("/Home/Error", "?code={0}");
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthorization();
    app.MapControllerRoute(
                            name: "default",
                            pattern: "{controller=Home}/{action=Index}/{id?}");
    app.Run();

Nothing of interest in appsettings.json:对 appsettings.json 没什么兴趣:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

The project file is as follows:工程文件如下:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <RootNamespace>MyFirstApp.MVC</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Dapper" Version="2.0.123" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.10" />
    <PackageReference Include="Microsoft.Data.Sqlite" Version="7.0.2" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.4" />
    <PackageReference Include="System.DirectoryServices.AccountManagement" Version="6.0.0" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="wwwroot\images\" />
  </ItemGroup>

</Project>

Below is the error log.下面是错误日志。 Logging added to confirm Controller action method is completing with no error:添加日志记录以确认 Controller 操作方法正在完成且没有错误:

info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: D:\Projects\ReportViewer
**info: ReportViewer.MVC.Controllers.HomeController[0]
      Into Index**
**info: ReportViewer.MVC.Controllers.HomeController[0]
      About to return view Index**
fail: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor[3]
      The view 'Index' was not found. Searched locations: /Views/Home/Index.cshtml, /Views/Shared/Index.cshtml
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.InvalidOperationException: The view 'Index' was not found. The following locations were searched:
      /Views/Home/Index.cshtml
      /Views/Shared/Index.cshtml
         at Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable`1 originalLocations)
         at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
         at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|30_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
      --- End of stack trace from previous location ---
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
      --- End of stack trace from previous location ---
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
         at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
         at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
fail: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor[3]
      The view 'Error' was not found. Searched locations: /Views/Home/Error.cshtml, /Views/Shared/Error.cshtml
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[3]
      An exception was thrown attempting to execute the error handler.
      System.InvalidOperationException: The view 'Error' was not found. The following locations were searched:
      /Views/Home/Error.cshtml
      /Views/Shared/Error.cshtml

I'd really appreciate any suggestions you may have.如果您有任何建议,我将不胜感激。

Thanks,谢谢,

Lance

Turns out the publish step in VS was failing to resolve some MVC Analyser dlls.结果是 VS 中的发布步骤无法解析某些 MVC 分析器 dll。 Despite this, it was still reporting the publish succeeded and generated the dlls and exe files.尽管如此,它仍然报告发布成功并生成了 dll 和 exe 文件。

Only spotted the publishing issue after scrolling through the publish output log for any clues.滚动浏览发布 output 日志以寻找任何线索后才发现发布问题。

Publishing problem was caused by having two versions of .NET6 installed on the dev machine.发布问题是由于在开发机器上安装了两个版本的 .NET6 引起的。 I removed the older version and updated do.net environment variables to point to do.net6 located in C:\Program Files....that broke the Dependency Frameworks in the MVP Project.我删除了旧版本并更新了 do.net 环境变量以指向位于 C:\Program Files.... 的 do.net6,这破坏了 MVP 项目中的依赖框架。 The Path and Version entries were blank for Microsoft.As.netCore.App and Microsoft.NETCore.App. Microsoft.As.netCore.App 和 Microsoft.NETCore.App 的路径和版本条目为空。

Under time pressure so created a new MVC project, copied all files across to this new project, published and deployed to IIS with no issues.在时间压力下,创建了一个新的 MVC 项目,将所有文件复制到这个新项目,发布并部署到 IIS,没有任何问题。

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

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