Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subfolder in Controllers ASP.NET MVC [duplicate]

In my Controllers folder i want to have a subfolder called Admin.

enter image description here

When i go to http://localhost:port/Admin/Login/ it says the page could not be found.

RouteConfig.cs

using System.Web.Mvc;
using System.Web.Routing;

namespace ICT4Events
{
    public class 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 }
            );
        }
    }
}
like image 812
yooouuri Avatar asked Feb 08 '23 12:02

yooouuri


1 Answers

You could use the next route to handle your issue:

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

DON'T FORGET to change default value for controller = "Home" to controller where you want to redirect when user types http://localhost:port/Admin/.

So when you go to http://localhost:port/Admin/Login/ you will use Login controller and Index action in the Admin folder.

IMPORTANT Also put this route BEFORE default route, because if you put this code after your "Default" route ASP.NET will read your http://localhost:port/Admin/Login/ like URL with Admin controller and Login action.

like image 71
user2771704 Avatar answered Feb 12 '23 10:02

user2771704