29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Figure 15-11. The error displayed when there are two controllers with the same name<br />

The MVC Framework searched for a class called HomeController and found two: one in the original<br />

RoutesAndUrls.Controllers namespace and one in the new<br />

RoutesAndUrls.AdditionalControllers namespace. If you read the text of the error shown in Figure 15-11,<br />

you can see that the MVC Framework helpfully reports which classes it has found.<br />

This problem arises more often than you might expect, especially if you are working on a large MVC project that uses libraries<br />

of controllers from other development teams or third-party suppliers. It is natural to name a controller relating to user accounts<br />

AccountController, for example, and it is only a matter of time before you encounter a naming clash.<br />

To address this problem, I can tell the MVC Framework to give preference to certain namespaces when attempting to resolve<br />

the name of a controller class, as demonstrated in Listing 15-25.<br />

Listing 15-25. Specifying Namespace Resolution Order in the RouteConfig.cs File<br />

using System;<br />

using System.Collections.Generic;<br />

using System.Linq;<br />

using System.Web;<br />

using System.Web.Mvc;<br />

using System.Web.Routing;<br />

namespace UrlsAndRoutes {<br />

public class RouteConfig {<br />

public static void RegisterRoutes(RouteCollection routes) {<br />

384<br />

routes.MapRoute("MyRoute", "

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!