Currently I've found that it's most convenient to have a separate public static void RegisterRoutes(RouteCollection routes)
method on each of my controllers. Then in Global.asax's RegisterRoutes
, I call all of these methods.
Of course, this is quickly getting out of hand.开发者_如何学C Every time I add a new controller, I have to go and update RegisterRoutes
with a boilerplate MyNewController.RegisterRoutes(routes)
call.
My first instinct is to make all of my controllers derive from some abstract ControllerBase
which has a RegisterRoutes
method defined, then use some kind of reflection to find all ControllerBase
-subclasses and call their static method.
My second instinct was to realize this is probably a solved problem, especially since it sounds suspiciously like this IoC-type stuff I've started to learn about very recently. So after some Googling didn't really turn anything up (possibly due to bad keywords), I thought I'd ask StackOverflow.
I use Mvc Turbine to register my routes. You could have each controller implement IRouteRegistrator
and register the routes you need there.
精彩评论