Routing not working when using areas in MVC.

November 5, 2017 at 5:29 PMMadestro

I recently came across a situation where I had two controllers named Home. One existed under an area and the other one under the normal Controllers folder.

The routes started misbehaving and sometimes they would fail.

I found a few articles about adding namespaces but that didn't seem to work.

After hours of searching and thinking, I figured it out. The namespaces have to match the location of the Controllers folder. Here is a sample to illustrate:

context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { controller = "System", action = "Index", id = UrlParameter.Optional}
                , new[] { "MyWebApp.Admin.Controllers" }
            );

 

Notice the namespace includes the the Controllers folder.

It's not just any name. It has to match your Controllers namespace exactly.

Posted in: ASP.NET MVC

Tags: ,