ASP MVC 文件夹层次结构
我有一个相当大的 ASP MVC 应用程序。 我宁愿创建一些层次结构,而不是将许多控制器都放在控制器目录中。 所以我可能有类似的事情,
~\Controllers\Security\
~\Controllers\Maintenance\
~\Controllers\Reports\
我也希望能够对视图进行类似的操作,
~\Views\Security\Users\
~\Views\Security\Roles\
~\Views\Maintenance\Customer\
~\Views\Maintenance\Product\
这很容易完成吗?
I have a fairly large ASP MVC application. Instead of having many controllers all in the controller directory I would rather create some hierarchy. So I might have something like
~\Controllers\Security\
~\Controllers\Maintenance\
~\Controllers\Reports\
I would also like to be able to do similar with Views
~\Views\Security\Users\
~\Views\Security\Roles\
~\Views\Maintenance\Customer\
~\Views\Maintenance\Product\
Is this easily done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您正在寻找类似“大师”在这篇文章中所说的内容:
http://haacked.com/archive/0001/01/01/areas-in-aspnetmvc.aspx
基本上,您必须创建一个 ViewEngine 来指定在哪里查找视图。 这是一个相当简单的代码,只是不要忘记将其注册到 global.asax 中! 至于控制器部分,您还必须在 global.asax 中注册新路由。
I think you're looking for something like what the "master" says in this post:
http://haacked.com/archive/0001/01/01/areas-in-aspnetmvc.aspx
Basically you have to create a ViewEngine to specify where to look for the views. It's a fairly simple code, just don't forget to register it in the global.asax! As for the controller part you'll have to register new routes also in the global.asax.
您正在搜索的概念称为“区域”,如 Phil Haack 在此概述的:
http://haacked.com/archive/2008/11/ 04/areas-in-aspnetmvc.aspx
The concept you're searching for is called "areas", as outlined by Phil Haack here:
http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx
我认为你需要编写自己的 RouteHandler,这应该不会太难。
快速谷歌搜索出现:这篇博文详细介绍了它
I would think you'd need to write your own RouteHandler, which shouldn't be too tough.
A quick google search turned up: This blog post detailing it
您是否考虑过切换到功能文件夹。 我已经尝试过(几乎没有修改)并且效果很好。
这篇文章中描述了
http://timgthomas.com/2013/10/feature- folders-in-asp-net-mvc/
代码示例位于 Jimmiy Bogard 的存储库中
https://github.com/jbogard/presentations/tree/master/putyourcontrollersonadietv2
Have you considered to switch to Features Folder. I've tried it (with little modifications) and it works pretty good.
Described in this post
http://timgthomas.com/2013/10/feature-folders-in-asp-net-mvc/
Code examples are in Jimmiy Bogard's repo
https://github.com/jbogard/presentations/tree/master/putyourcontrollersonadietv2
您真正想要的是让您的视图文件夹层次结构与控制器的命名空间层次结构相匹配。 您可以编写一个自定义 ViewEngine 来相当轻松地完成此操作 - 有关详细信息,请参阅我的 GitHub 上的 ControllerPathViewEngine 项目。
我包含了 ControllerPathRazorViewEngine 类的一个片段来概述它的工作原理。 通过拦截 FindView / FindPartialView 方法并将控制器名称替换为文件夹路径(基于控制器命名空间和名称),我们可以让它从主 Views 文件夹内的嵌套文件夹加载视图。
What you really want here is for your Views folder hierarchy to match the namespace hierarchy of your controllers. You can write a custom ViewEngine to do this fairly easily - see my ControllerPathViewEngine project on GitHub for details.
I've included a snippet of the ControllerPathRazorViewEngine class to outline how it works. By intercepting the FindView / FindPartialView methods and replacing the controller name with a folder path (based on controller namespace and name), we can get it to load views from nested folders within the main Views folder.