Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top