如何在运行时找出控制器属于 asp.net mvc 中的哪个区域?

发布于 2024-08-12 23:12:04 字数 217 浏览 7 评论 0原文

由于这个东西是相当新的,我找不到任何好的参考资料。

我想使用反射来枚举应用程序中的所有控制器。使用反射这并不难。但既然区域到位了。我如何知道特定控制器属于哪个区域(如果有)?

也许我做错了,也许我需要枚举该区域......那么我该怎么做呢?如果控制器不属于任何区域怎么办?有默认的吗?

有很多很好的文章深入解释了控制器和视图。如果有人可以向我指出类似的区域,我将不胜感激。

Since this stuff is fair new I couldn't figure out any good reference on it.

I want to use reflection to enumerate through all the controller in my application. This is not hard using reflection. But since area come into place. How do I know which area (if any) does a particular controller belong to?

Maybe I am doing it wrong, maybe I need to enumerate through the area instead... so then how do I do that? What if a controller doesn't belong to any area? Is there a default one?

There are many good write up out there that explain in depth about the controller and view. If somebody can point me to something similar for area I would greatly appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Bonjour°[大白 2024-08-19 23:12:04

您要么必须更改控制器所在的命名空间才能检测区域,要么从( RouteTable.Routes )循环中获取路线数据并尝试匹配数据令牌,即您在 {controller} 中放入的内容,以及/或 url 信息:

以下是获取路由信息的方法:

 foreach (RouteBase routeBase in RouteTable.Routes)
 {
      Route route = routeBase as Route;

      var routeUrl = route.Url;                
 }

Phil Haacks Route Debugger 可能会帮助您:
http://haacked.com/archive/2008/03/ 13/url-routing-debugger.aspx

关于区域的优秀 MSDN 文章:
http://msdn.microsoft.com/en-us /library/ee461420(VS.100).aspx

听起来很棘手,祝你好运!

You'll either have to change the namespace your controllers are in to detect the areas or grab the route data from ( RouteTable.Routes ) loop through it and try to match up the data tokens, aka what you put in {controller}, and/or the url information:

Here's how to get the route information:

 foreach (RouteBase routeBase in RouteTable.Routes)
 {
      Route route = routeBase as Route;

      var routeUrl = route.Url;                
 }

Phil Haacks Route Debugger may help you:
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

Good MSDN Article about Areas:
http://msdn.microsoft.com/en-us/library/ee461420(VS.100).aspx

Sounds tricky, good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文