ASP.NET MVC 4: Where Have All The Global.asax Routes Gone?
Wondering where all the global.asax routes have gone today? Read on and find out.
Join the DZone community and get the full member experience.Join For Free
i ran into this a few days back and had been meaning to blog about it, so here it finally is while it’s still interesting information.
in asp.net mvc 1.0, 2.0, and 3.0, routes are defined in the global.asax.cs file in a method called registerroutes(..).
it had become an almost unconscious navigate-and-click routine for me to open global.asax.cs up to diagnose routing errors and to introduce new routes. so upon starting a new asp.net mvc 4 application with visual studio 11 rc (or visual studio 2012 rc, whichever it will be called), it took me by surprise to find that the registerroutes method is no longer defined there. in fact, the mvcapplication class defined global.asax.cs contains only 8 lines of code! i panicked when i saw this. where do i edit my routes?!
what kept me befuddled for far too long (quite a bit longer than a couple seconds, shame on me!) was the fact that these lines of code, when not actually read and only glanced at, look similar to the application_start() from the previous iteration of asp.net mvc:
eventually i squinted and paid closer attention to the difference, and then i realized that the registerroutes(..) method is being invoked still but it is managed in a separate configuration class. is this class an application settings class? is it a poco class? a wrapper class for a web.config setting? before i knew it i was already right-clicking on registerroutes and choosing go to definition ..
under tools –> options –> projects and solutions –> general i have track active item in solution explorer enabled, so upon right-clicking an object member reference in code and choosing “go to definition” i always glance over at solution explorer to see where it navigates to in the tree. this is where i immediately found the new config files:
.. in a new app_start folder, which contains filterconfig.cs, routeconfig.cs, and bundleconfig.cs, as named by the invoking code in global.asax.cs. and to answer my own question, these are poco classes, each with a static method (i.e. registerroutes).
i like this change. it’s a minor refactoring that cleans up code. i don’t understand the naming convention of app_start, though. it seems like it should be called “config” or something, or else global.asax.cs should be moved into app_start as well since application_start() lives in global.asax.cs. but whatever. maintaining configuration details in one big global.asax.cs file gets to be a bit of a pain sometimes especially in growing projects so i’m very glad that such configuration details are now tucked away in their own dedicated spaces.
i am curious but have not yet checked to determine whether app_start as a new asp.net folder has any inherent behaviors associated with it, such as for example post-edit auto-compilation. i’m doubtful.
in future blog post(s), perhaps my next post, i’ll go over some of the other changes in asp.net mvc 4.
Published at DZone with permission of Jon Davis, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.