How do I find out at runtime what area a controller is belong to in asp.net mvc?
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 d开发者_开发问答oesn'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.
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!
精彩评论