1
votes

Trying to add an MVC route to my project to handle a subfolder with controllers. My project structure is as follows:

/Controllers/

  • HomeController
  • SomeOtherController

/Controllers/Admin

  • AdminController
  • SomeController

/Views

  • Home
  • SomeOther

/Views/Admin

  • Admin
  • Some

The issue is that the controller/views in the admin folders are not being found and giving a n HTTP404 error.

Here is my RouteConfig

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "Admin",
        url: "Admin/{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

I understand that I could use Areas, but I don't want to create a new MVC structure. I simply want to organize my controllers and views into an admin folder.

1
Could you try putting this route before the default, I know it sounds odd but thats just how i've always done it?Ashley Medway
Whoa, that worked! Why did it work? am I missing something or should it not matter the order? Anyways, post it as an answer so I can give you the credit. Thanks!devfunkd
Add as the answer, I honestly have no idea. Was just how I was taught to do it and have blindly followed. I will do some research and try to pad the answer out a bit :)Ashley Medway

1 Answers

1
votes

Could you try putting this new route before the default.

routes.MapRoute(
    name: "Admin",
    url: "Admin/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

The importance of route order.

It worked because routes are evaluated from top to bottom, as name: "Default", was at top it was handing every thing – Satpal