简体   繁体   English

转换为 AS.NET Core 3.1 后 MVC 找不到视图

[英]MVC can't find Views after converting to ASPNET Core 3.1

We are converting an MVC/Angular web site from AS.NET Core 2.2 to AS.NET Core 3.1.我们正在将 MVC/Angular web 站点从 AS.NET Core 2.2 转换为 AS.NET Core 3.1。 I have got the build to run with no errors or warnings, but when it starts up (in Visual Studio 2019), it looks for Home/Index, finds the controller fine, and looks for the Home/Index View, but it fails to find it even though it is there (in Views/Home/Index.cshtml), right where it's supposed to be (and right where it's been for the several months that we have been developing this site. I have it now reduced to a test case that I can set the web project file to.netcoreapp2.2 and the site runs fine, then stop it and change the to.netcoreapp3.1, with no other changes, and it fails. The errors are exactly what you would expect if the file were missing:我让构建运行时没有错误或警告,但是当它启动时(在 Visual Studio 2019 中),它会查找 Home/Index,找到 controller 正常,并查找 Home/Index 视图,但它无法找到它,即使它在那里(在 Views/Home/Index.cshtml 中),就在它应该在的地方(而且在我们开发这个网站的几个月里它一直在的地方。我现在把它简化为一个测试如果我可以将 web 项目文件设置为 .netcoreapp2.2 并且站点运行正常,然后停止它并更改为 .netcoreapp3.1,没有其他更改,它失败了。这些错误正是你所期望的,如果文件丢失:

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

/Views/Home/Index.cshtml /Views/Home/Index.cshtml

/Views/Shared/Index.cshtml /Views/Shared/Index.cshtml

/Pages/Shared/Index.cshtml /Pages/Shared/Index.cshtml

But the file is there.但是文件在那里。 I'm completely baffled.我完全不知所措。

Can anyone give me a hint as to what's going on?谁能告诉我发生了什么事?

Thanks.谢谢。

We just had a similar issue with partials: after upgrading a web app from core 2.2 to 3.1 partial views could not be found.我们刚刚遇到了类似的部分问题:将 Web 应用程序从核心 2.2 升级到 3.1 后,找不到部分视图。 The solution for us was to replace this...我们的解决方案是更换这个......

services.AddMvc();

... with ... ... 和 ...

services.AddControllersWithViews();
services.AddRazorPages().AddRazorRuntimeCompilation();

Although I believe .AddMvc() is equivalent to .AddControllersWithViews() plus .AddRazorPages it doesn't include the runtime compilation part out of the box.虽然我相信 .AddMvc() 等同于 .AddControllersWithViews() 加上 .AddRazorPages 它不包括开箱即用的运行时编译部分。

Check the part about routing adjustments - see Migrate from ASP.NET Core 2.2 to 3.0查看路由调整部分 - 参见Migrate from ASP.NET Core 2.2 to 3.0

Most likely you need to change this in your Startup class:您很可能需要在Startup类中更改此设置:

app.UseMvc(routes =>
    {
        routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
    });

with that:接着就,随即:

app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
    });

replace代替

`app.UseMvc(routes =>
        {
            routes.MapRoute(
               name: "areas",
              template: "{area=User}/{controller=Home}/{action=Index}/{id?}");
        });`

With

`app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });`

They replaced the app.UseMvc to app.UseEndponts when migrating from .Net Core 2 to .Net Core 3.他们迁移从.net的Core 2到.NET核心3时更换app.UseMvcapp.UseEndponts。

And also if you have another Views Folder, copy the _ViewImports.cshtml and _ViewStart.cshtml from the existing Views folder to the other Views Folders.此外,如果您有另一个视图文件夹,请将_ViewImports.cshtml_ViewStart.cshtml从现有视图文件夹复制到其他视图文件夹。

I had same problem.我有同样的问题。

For me it was this in project file:对我来说,它在项目文件中是这样的:

<RazorCompileOnBuild>false</RazorCompileOnBuild>
<RazorCompileOnPublish>false</RazorCompileOnPublish>

I had to remove it.我不得不删除它。

First, you have to install package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation :首先,你必须安装 package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

install-package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

Then add AddRazorRuntimeCompilation() after services.AddControllersWithViews() line in startup.cs :然后在startup.cs中的services.AddControllersWithViews()行之后添加AddRazorRuntimeCompilation()

services.AddControllersWithViews().AddRazorRuntimeCompilation();

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

相关问题 ASPNET Core mvc 3.1 表单在 POST 上始终为空 - ASPNET Core mvc 3.1 form always null on POST ASP.NET Core 3.1 MVC 应用程序 - 在子目录中搜索视图 - ASP.NET Core 3.1 MVC app - search views in subdirectory 如果我的 aspnet mvc 应用程序以“.NET Core 3.1”为目标,它会针对哪个版本运行? - What point release(s) does my aspnet mvc app run against if it targets “.NET Core 3.1”? AspNet Core 3.1 中的 FromFormAttribute 和反序列化问题 Api - Problems with FromFormAttribute and deserialization in AspNet Core 3.1 Api 为什么 Automapper 在映射列表时返回 null<t> 在 aspnet.core 3.1 中?</t> - Why Automapper returns null while mapping List<T> in aspnet.core 3.1? 从 .NET Core 2.1 升级到 3.1 后,当我的应用程序在 E2E 测试中启动时,找不到视图 - After upgrading from .NET Core 2.1 to 3.1 Views aren't being found when my app starts in E2E tests UWP 无法使用 EF 核心 3.1 构建 - UWP can't build with EF core 3.1 AspNet Core API无法在同一控制器上获得两个GET - AspNet Core API can't get two GETs on same controller PDF 未下载 MVC dotnet core 3.1 “无法下载”错误 - PDF not downloading MVC dotnet core 3.1 “Couldn't download” error 自定义区域中的 ASP.NET Core 3.1 MVC 视图与默认区域中的视图设计不同 - ASP.NET Core 3.1 MVC Views in custom Areas dont have the same design as those in the default one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM