13

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?

4
  • 2
    you have to specify your controller namespace name in Map.Route so it can locate the controller. Refer this post stackoverflow.com/questions/17178688/controller-in-sub-folder Also, move your admin route at first postion as routing should be define most specific to least specific. Commented Jan 17, 2017 at 17:32
  • @KD in my RouteConfig.cs file? Commented Jan 17, 2017 at 17:35
  • Follow this link: stackoverflow.com/questions/33802430/… Commented Jan 17, 2017 at 17:35
  • 1
    I'll be the first to bite: unless you have a requirement to do this, don't do it. Stop. You're just fighting MVC. You aren't making it do anything new or adding value for your company or employer. Commented Jan 18, 2017 at 21:52

4 Answers 4

9

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.

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

3 Comments

I tried your code, new error: The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
this is actually Great !! it means your Url is working now.. Next issue we have is to help MVC to locate view path.. it must have provided you list of paths its searched. Now we need to make sure it is considering our path. As i see your view is located at Views/admin/homeadmin/. we need to make sure that MVC should search this path to find associated view
can you please refer following link to add this path? both the answers provided in this link are fine to go with. Let me know if you need help regarding this. stackoverflow.com/questions/799838/…
7

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".

2 Comments

What do you mean by "Areas"? Is it a new concept in VS?
@KonradViltersten Yes, this is concept in MVC. Area allows to partition large application into smaller units where each unit contains separate MVC folder structure and takes care of routing of controller and rendering of views within Area automatically.
3

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.