Monday, May 7, 2012

Renaming an ASP.NET MVC solution in Visual Studio

The Platinum Bay blog has a good writeup on how to rename a Visual Studio project correctly (so that the underlying folders are renamed and re-hooked up). 

But I still ran into problems - an MVC project that I renamed returned the following error :
Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter. 
The request for 'Home' has found the following matching controllers:
(new project namespace).Controllers.HomeController
(old project namespace).Controllers.HomeController
A file system search even turned up nothing, but I finally figured out the solution while reading a great in-depth article on controllers by Scott Allen, where he states:
...if you tell the factory to look for a "Home" controller, the factory can return a newly instantiated instance of a HomeController class regardless of the namespace or assembly it lives in—as long as it implements IController
If you've build the old project, you'll probably have old debugging dlls in the project's "bin" folder, so MVC will search and find those. Clear them out and rebuild, and everything should work. By the way, you could actually just follow the instructions in the error, and specify your route like this:

                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] {"(new project namespace)"}

but it's cleaner to delete the old .dlls, as they could potentially cause you other problems as well. I'm very surprised that even the VS 11 beta still doesn't rename solutions correctly out of the box.

No comments: