user979331
user979331

Reputation: 11851

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?

Upvotes: 12

Views: 27943

Answers (4)

Darkseal
Darkseal

Reputation: 9564

Assuming you're using MVC5 I would definitely consider using the attribute-based routing feature of ASP.NET MVC in the following way:

1) Call the routes.MapMvcAttributeRoutes() method in your /App_Start/RouteConfig.cs file:

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

        routes.MapMvcAttributeRoutes();

        // other routes.MapRoute() settings
    }
}

2) Use the [Route] attribute in your "subfolder" Controller in the following way:

[Route("Admin/HomeAdmin/{action}")]
// URL: /Admin/HomeAdmin/YourActionName
public class HomeAdminController : GestioneController
{
    // TODO: Put your Action Methods here
    // They will respond to 
}

In case you need additional info check out this blog post that I wrote on this topic.

Upvotes: 0

Skyrider
Skyrider

Reputation: 111

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.

Upvotes: 3

LKC
LKC

Reputation: 186

As per the MVC architecture, a view is rendered from subfolder named as controller name inside Views folder. I don't think nesting of folder inside Views will work for you. Instead if you want to organize your folders you can choose "Areas".

Upvotes: 7

K D
K D

Reputation: 5989

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.

Upvotes: 8

Related Questions