4

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 }
            );
        }
    }
}
6
  • Sounds like you have a routing issue. There is a decent intro to routing here: asp.net/mvc/overview/older-versions-1/controllers-and-routing/… If you can provide what is inside your route.config file, I may be able to help some more Commented Nov 19, 2015 at 11:21
  • Routing is controlled by added routes, not by folder structure. Commented Nov 19, 2015 at 11:21
  • Also, if using MVC 5, you could have a crack at attribute routing. I much prefer it to the conventional way blogs.msdn.com/b/webdev/archive/2013/10/17/… Commented Nov 19, 2015 at 11:23
  • Do you have a corresponding "Admin" folder in "Views" directory? Commented Nov 19, 2015 at 11:25
  • @DavidWatts see post Commented Nov 19, 2015 at 11:25

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

Your new route "SubFolder" does not include the possibility of including an action in the route (in your case, "Admin").

Your url wants to match routie like

"SubFolder/ChildController/{action}"

If don't include the "{action}" in your route, it won't match your route. It then tries the default route, which obviously fails.

Try adding "{action}" to your route as shown in the below example

routes.MapRoute(
"SubFolder", // Route name
"SubFolder/ChildController/{action}",
new { controller = "ChildController", action = "Index" },
new[] { "Homa.Areas.Kiosk.Controllers.SubFolder" });

2 Comments

I need to add like 5 Maproutes for 5 different controllers?
One route is enough for all the five controllers. Routing is just pattern matching mechanism. The route values for controller and action specified in the example code posted by me is default route. If you dont give values for controller or action then default route will fire.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.