如何指定为 Mvc 控制器创建 Mvc 视图的位置?
重要更新
自 MVC 2.0 Preview 1 发布以来,此功能已以区域的形式作为实际框架本身的一部分实现。 更多详细信息请访问 Phil Haack 的博客 此处
我有一个名为 ListManagerController 的控制器。 该控制器包含一个名为 Index() 的 ActionResult 方法。 当我在 Visual Studio 中右键单击“索引”并选择“添加视图”时,将在 /Views/ListManager/Index 中创建新视图。
不过,我希望在 /Views/Manage/ListManager/ 中创建索引视图和所有后续视图。 我将如何实现这个目标?
编辑:有人指出,这个问题与发布的问题重复此处。 看来我的搜索技巧一开始就失败了。
Important Update
Since the release of MVC 2.0 Preview 1 this feature has been implemented as the part of the actual framework itself in the form of Areas. More details available on Phil Haack's blog here
I have a controller called ListManagerController. This controller contain an ActionResult method called Index(). When I right cick on Index in Visual Studio and select Add View the new view is created in /Views/ListManager/Index.
However I want the Index view and all subsequent views to be created in /Views/Manage/ListManager/. How would I accomplish this?
Edit: It was pointed out that this question is a duplicate of the question posted here. It seems my searching skills failed me initially.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
视图的位置与您正在使用的 ViewFactory 相关联。 据我所知,网络表单视图引擎不支持区域[在您的示例中进行管理]。
Spark 支持此功能并且非常干净,您还可以混合搭配 Web 表单和 Spark 视图,这样您就不需要重新创建您的所有视图。
更新: 看起来 Phil Haack 有一个 博客文章。 他的代码是针对 RC 的,但我认为应该可以很好地针对 ASP.NET MVC RTM 进行编译。
The location of views is tied to the ViewFactory you are using. AFAIK the web forms view engines does not support areas [Manage in your example].
Spark supports this and is very clean, you can also mix and match web forms and spark views so you don't have to recreate all your views.
UPDATE: Looks like Phil Haack has a blog post on how to achieve this. His code is for the RC, but I think that should compile fine against ASP.NET MVC RTM.
我知道您已经接受了答案,但这是我在 Phil Haack 的帖子。
首先你需要有自己的ViewEngine来查找View文件夹下的文件夹。 像这样的东西:(您会注意到它看起来很像 Phil Haack 的区域代码)
然后在您的
Global.asax
中将Application_Start
上的默认 ViewEngine 替换为您的自定义 ViewEngine > :现在,当您在 Global.asax 中定义路由时,您需要设置一个 root 值,指示要在 View 文件夹下查找的文件夹,如下所示:
I know you already accepted an answer but here's what I came up with while experimenting with the same idea, with the help of Phil Haack's post.
First you need to have your own ViewEngine to look for folders under View folder. Something like this : (You'll notice that it looks a lot like Phil Haack's areas code)
Then in your
Global.asax
replace the default ViewEngine with your custom one, onApplication_Start
:Now when you are defining routes in
Global.asax
, you need to set aroot
value indicating the folder to look for under the View folders like so :这个问题非常重复这个问题,
所以我将在这里引用我对该问题的回答。
This question is VERY much a repeat of this question
so I'll quote my answer to that one here.