Thursday, March 11, 2010

ASP.NET MVC2 RC2 Areas Namespace Conflict

While converting an MVC1 app to MVC RC2 I created an Area and moved my existing controllers into said area. Resharper identified the “Namespace does not correspond to file location, should be: AAA.Areas.BBB.Controllers” as expected. A simple “Move declarations to namespace ‘AAA.Areas.BBB.Controllers’” was sufficient to alleviate the issue.

At this point I also realised I needed to change my default namespace for the project to “XXX.AAA” and hence the namespace of my controller needed to change to “XXX.AAA.Areas.BBB.Controllers”. No problem.

Tried to navigate to said controller and received http 404, “The Resource cannot be found”. Debugged and found the route was being registered via AreaRegistraion.RegisterArea but the controller constructor wasn’t being hit.

Fix 1 (The hard way) : context.MapRoute(name, url, defaults, namespaces) allows you to specify the controller’s namespace/s as outlined by haacked.

Fix 2 (The easy way): make sure the area registration uses the same namespace as the controller i.e. “XXX.AAA.Areas.BBB” instead of the original “AAA.Areas.BBB”.

Now everyone’s happy :)