Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Controller SubFolder

I am using ASP.NET MVC and trying to create a controller subfolder. I have looked at other post on this site and tried what I was able to find online, but it still running into this issue:

HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

The screenshot below is the subfolder I created in my controller folder.

enter image description here

and here is a screenshot of my View folder.

enter image description here

And here is what I tried in my RouteConfig.cs file

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: "AdminSubForder",
        url: "admin/{controller}/{action}/{id}",
        defaults: new { controller = "HomeAdmin", action = "Index", id = UrlParameter.Optional }
    );
}

But my subfolder still doesn't work. What am I doing wrong here?

like image 503
user979331 Avatar asked Jan 17 '17 17:01

user979331


Video Answer


2 Answers

Usually, when you directly add a controller (or any class file) into a folder (or sub folder), Visual Studio will modify the namespace in the class file to match that folder. So, in your case, instead of having the 'myprojectname.controller' namespace in your class, it will have the 'myprojectname.controller.admin' namespace.

The solution? Well, I do this all the time and have controllers inside a bunch of folders to organize my code. The easiest way is to add the controller inside the "Controller" folder first. That way it will have the proper namespace. Then, just drag and drop the file into the folder you want to organize it in. So, whenever you create a controller, make sure you create it in the "Controller" folder. I just right click on the "Controller" folder and create the controller. Then drag the file into whatever folders you want.

like image 158
Skyrider Avatar answered Sep 25 '22 01:09

Skyrider


try following things...

first define your routing in following manner... The routing should be defined from Most Specific to Least Specific patterns

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

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


        }

if it still doesn't work, try to add assembly name of your controller as mentioned in following post.. Controller sub folder

Also, let us know what URL you are typing to reach the page.

like image 37
K D Avatar answered Sep 21 '22 01:09

K D