Reputation: 5033
I'm trying to improve the speed at which my MVC2 app is starting up.
I did a first round of performance sampling, and it appears that the
MvcAreaRegistration.RegisterAllAreas
is taking up most of the startup time.
I read here that you can manually register the area's as well, and I would like to try that out, but I'm not sure how the syntax works on that page.
So my (first) question woud be: how can I register my Area's manually?
Upvotes: 15
Views: 5240
Reputation: 96
You can do this completely by hand and avoid using RegisterArea implementations.
Check this article: http://www.philliphaydon.com/2011/07/mvc-areas-routes-order-of-routes-matter/
In short - you need to add "area" DataToken to your route:
private void RegisterAreas(RouteCollection routes)
{
// AreaRegistration.RegisterAllAreas();
var route = routes.MapRoute(
"MyArea_Default",
"MyArea/{controller}/{action}/{id}",
new { controller = "App", action = "Index", id = UrlParameter.Optional },
new string[] { "MyProject.Areas.*" }
).DataTokens.Add("Area", "CDR");
}
Upvotes: 1
Reputation: 24125
First prepare yourself a helper method in Global.asax like this:
private static void RegisterArea<T>(RouteCollection routes, object state) where T : AreaRegistration
{
AreaRegistration registration = (AreaRegistration)Activator.CreateInstance(typeof(T));
AreaRegistrationContext registrationContext = new AreaRegistrationContext(registration.AreaName, routes, state);
string areaNamespace = registration.GetType().Namespace;
if (!String.IsNullOrEmpty(areaNamespace))
registrationContext.Namespaces.Add(areaNamespace + ".*");
registration.RegisterArea(registrationContext);
}
Now you can use this helper method for manual registration in Application_Start like this:
//Replace AreaRegistration.RegisterAllAreas(); with lines like those
RegisterArea<FirstAreaRegistration>(RouteTable.Routes, null);
RegisterArea<SecondAreaRegistration>(RouteTable.Routes, null);
The AreaRegistration classes are created by Visual Studio when you add new Area, you can find them in Areas/AreaName directories.
Upvotes: 5
Reputation: 6009
Try this super handy area registration utility. Not only does it make registration easier, but also way faster since it doesn't scan every loaded assembly for areas.
Upvotes: 6