ASP.NET MVC 2 中可以嵌套区域吗?
我想创建一个带有嵌套区域的项目结构。例如,我有一个“主页”区域,在其下方,我想要“新闻”区域来处理它自己的路线注册,并且当路线指向“新闻”区域中的控制器时,将正确找到视图。通过将“新闻”区域名称更改为“主页/新闻”而不是简单的“新闻”,可以找到正确的视图。在主 Global.asax.cs 中,我实例化所有区域(“Home”和“News”)并单独注册它们,因为 RegisterAllAreas() 函数仅查找一层深度的区域(即,它仅查找并注册“Home”和“News”) “ 区域)。
还有其他人尝试过类似的事情吗?这是一个重大黑客攻击还是一个稳定的长期解决方案?非常感谢您提供的任何建议。
I would like to create a project structure with nested areas. For instance I have a "Home" area and underneath this I would like the "News" area that handles it's own route registration and will properly find views when a route points to a controller in the "News" area. By changing the "News" area name to be "Home/News" instead of simply "News", the proper views are found. In the main Global.asax.cs, I instantiate all of the areas ("Home" and "News") and register them individually because the RegisterAllAreas() function only finds areas one level deep (i.e. it only finds and registers the "Home" area).
Has anyone else tried something similar? Is this a major hack or can this be a stable long-term solution? Any advice you can offer is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 MvcCodeRouting(一个单独的开源 CodePlex 项目)来完成此操作。
You can do it with MvcCodeRouting, a seprate open-source CodePlex project.
我相信使用控制器创建类似的东西不会有问题,因为它们是使用命名空间找到的。
问题出在观点上。
默认情况下,MVC 路由(通过 ViewEngine)仅使用 RouteData 集合中的 Area、Controller 和 View 值。
这是在 VirtualPathProviderViewEngine 中的虚拟 FindView 方法中实现的(并使用非虚拟 GetPath)。您必须重写 ViewEngine 中的 FindView 方法。
这不是一个非常大或复杂的代码量,但你最好的选择是浏览源代码并窥探周围,因为有一些缓存和其他一些东西......
I believe that creating something like this with the Controllers won't be a problem, because they're found using namespace.
The problem is with the views.
By default the MVC routing (through the ViewEngine) only uses the Area, Controller and View values in the RouteData collection.
This is implemented in the VirtualPathProviderViewEngine in virtual FindView method(s) (and using non virtual GetPath). You'll have to override FindView methods in your ViewEngine.
It's not a very large or complicated amount of code, but your best bet is going through the source and snooping around, because there's some caching going around and some other things...