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.

firefly
  • 285
  • 3
  • 12
  • Can I ask why you need to know the area? If you are writting your application in a RESTful manner then should the area matter? Is there a specific reason why you need this? I ask because maybe there is another and/or better way to do what you are trying to accomplish. – griegs Nov 28 '09 at 05:52
  • I am writing a user manager so Forum/Home/Edit need to have different permission than Home/Edit Two controller obviously have the same name but one is at the root and the other one inside the forum area. – firefly Nov 28 '09 at 20:23

1 Answers1

1

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!

John Farrell
  • 24,673
  • 10
  • 77
  • 110
  • I've thought of this two approach before though neither seem elegant enough hence I posted the question here. Using the namespace is nice, convention over configuration kinda thing, but it's rather fragile. Using the RouteTable might work but it seem a little hackish to me. Either way I will play around with it some more and report back :) – firefly Dec 08 '09 at 04:02