开发者

How can I use some kind of IoC (?) to automatically register routes for my ASP.NET MVC controllers?

开发者 https://www.devze.com 2023-01-14 15:07 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消