c# - 如何解决 ASP.NET Core MVC 中的 'View not found' 错误?

标签 c# asp.net-core asp.net-core-mvc asp.net-core-3.1 asp.net-core-routing

我不知道为什么我在 ASP.NET Core MVC 3.1 中每次都会收到这个错误。我有一个观点,它就在它的位置。我只在某些区域遇到此错误。

 InvalidOperationException: The view 'Dashboard' was not found. The following locations were searched:
    /Areas/Admin/Views/Home/Dashboard.cshtml
    /Areas/Admin/Views/Shared/Dashboard.cshtml
    /Views/Shared/Dashboard.cshtml

最佳答案

十分之九, View 上的此错误是因为您忘记将 [Area("Admin")] 属性放在区域 Controller 的顶部。

Note: As you noted in your answer, this should not include the suffix Area.

虽然按照惯例,您的区域 View 是在 /Areas/{Area}/Views/ 中搜索的,但没有任何努力来隐式确定 Controller 适用于某个区域。换句话说,.NET 完全依赖于 [Area()] 属性,而与您的 Controller 是否位于例如/Areas/{Area}/Controllers/。出于组织目的,您可能无论如何都会将它们存储在那里,但 .NET 不关心这两种方式。

关于c# - 如何解决 ASP.NET Core MVC 中的 'View not found' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64992397/

相关文章:

c# - DataContext - ListView - 刷新 UI - INotifyPropertyChanged

c# - Unity2D小行星风格游戏

c# - 如何从组合框中删除更多项目?

c# - 检查 GPS 和互联网是否在 Windows 8 Phone 应用程序中打开?

c# - 将数据从 ModelBinder 传递到自定义 InputFormatter

c# - 抽象模型的 Asp.Net Core 2.0 (MVC) Content Binder

c# - 选择 Tag Helper 获取值和文本

asp.net-core - 找不到类型或命名空间名称 'IWebHostEnvironment'(是否缺少 using 指令或程序集引用?)

asp.net-core - 元素内的条件样式

asp.net - ASP.NET Core 中的性能计数器在哪里?