使用自己的Model View Controller文件夹创建组件。并为他们改变路线?

时间:2012-11-03 14:16:15

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我的问题是我有一个MVC4项目,我想创建一些我希望使用部分视图在一些视图中添加的组件。我将向您展示我想在MVC项目中创建的文件夹逻辑图片。

enter image description here

从这里我怎么能添加这些部分视图,因为当我尝试访问它们时,它们首先在我的应用程序的主视图文件夹中查找,有没有办法让我的应用程序使用custum路由?

@{ Html.RenderPartial("Components\\Chart\\View\\Index.cshtml");  }

这会产生这样的错误。

  

找不到部分视图'Conmponents \ Chart \ View \ Index.cshtml'   或者没有视图引擎支持搜索的位置。下列   搜索地点:   〜/浏览/首页/ Conmponents \图\查看\ Index.cshtml.aspx   〜/浏览/首页/ Conmponents \图\查看\ Index.cshtml.ascx   〜/查看/共享/ Conmponents \图表\视图\ Index.cshtml.aspx   〜/查看/共享/ Conmponents \图表\视图\ Index.cshtml.ascx   〜/浏览/首页/ Conmponents \图\查看\ Index.cshtml.cshtml   〜/浏览/首页/ Conmponents \图\查看\ Index.cshtml.vbhtml   〜/查看/共享/ Conmponents \图表\视图\ Index.cshtml.cshtml   〜/查看/共享/ Conmponents \图表\视图\ Index.cshtml.vbhtml

1 个答案:

答案 0 :(得分:1)

您可以使用~作为项目目录指定路径。

Html.RenderPartial("~/Components/Chart/View/Index.cshtml",model)

此问题也与路由无关。路由是将传入请求映射到您的操作方法。 ASP.NET MVC尝试使用控制器和操作名称查找您的视图。如果它不存在,它还会检查共享文件夹。您可以通过指定精确路径来覆盖此行为,如上例所示。

<强>更新

此外,您还必须对View文件夹进行一些配置。在默认的Views文件夹中复制Web.config 或将所有视图配置移动到项目的Web.config文件应该可以解决问题。