[英]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.UseMvc到app.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.