asp.net-mvc - 找不到名为 'XX' 的 View 组件

标签 asp.net-mvc asp.net-core-mvc

我正在使用 aspnet 5 和 MVC 6。

我已经使用 HERE 中的说明创建了一个 View 组件但是当我运行我的网站时出现错误

找不到名为“XX”的 View 组件。 在

@Component.Invoke("XX"))

这是我的项目的结构

namespace Test
{
public class XXViewComponent : ViewComponent
{
   public IViewComponentResult Invoke()
    {       
        return View();
    }
}

}

然后在 View 文件夹中我有 Views/Assets/index.cshtml

<div>
    <div class="col-md-4">
        @Component.Invoke("XX"))
    </div>
</div>

vc位于Views/Assets/Components/XX/default.cshtml

<div class="col-md-6" id="listOfCustomers">
    <table class="table table-condensed">
      <span> Test partial View</span>
    </table>
</div>

最佳答案

我也遇到过类似的问题。 对我有帮助的是您自己使用的页面的底部部分(Avoiding magic strings):

  • 从类名称(以及构造函数)中删除“ViewComponent”后缀 - 然后您将拥有:

    public class XX : ViewComponent
    
  • 调用 View 组件时使用nameof:

    @Component.Invoke(nameof(XX))
    
  • 不要忘记在主视图 (Views/Assets/index.cshtml) 中使用 View 组件的 using - 在您的情况下 @using Test

这可能既可以帮助您,又可以保存组件编译时的调用。

正如 Joe Audette 指出的那样, View 可能位于不寻常的位置,但这会导致不同的错误消息。

关于asp.net-mvc - 找不到名为 'XX' 的 View 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33923502/

相关文章:

c# - ASP.NET Core 表单 POST 导致 HTTP 415 Unsupported Media Type 响应

c# - 未在 ASP.NET 5 应用程序上点击错误页面

.net - 开发一个在 Mono 和 Windows 下运行的 ASP.Net web 应用程序有多难?

c# - 这个 MVC 项目中的第一行是什么意思?

asp.net-mvc - 您如何使用 ASP.NET MVC 为请求计时?

c# - 如何在#if 语句中设置.NET Core 进行编译

asp.net-mvc - 直接nuget在哪里放置文件

c# - MVC : POST controller method get empty Dictionary

azure - 在 Azure Web 应用程序中显示 ASP.NET 5 错误页面

c# - 升级到 ASP.NET MVC6 beta 7 后打开现有项目时出现异常