ASP.NET MVC SubFolder Controller

I am using ASP.NET MVC and trying to create a subfolder of the controller. I looked at another post on this site and tried what I managed to find on the Internet, but it still faces this problem:

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 a subfolder that 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 the 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 is still not working. What am I doing wrong here?

+5
source share
4 answers

try the following things ...

first define your routing as follows ... Routing should be determined using the most specific to the least specific models

 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 does not work, try adding the assembly name of your controller, as indicated in the next post. Controller subdirectory

Also tell us which URL you type to access the page.

+4
source

In accordance with the MVC architecture, the view is displayed from a subfolder named as the controller in the Views folder. I do not think that nesting a folder inside Views will work for you. Instead, if you want to organize your folders, you can select "Locations."

+2
source

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

Decision? Well, I do this all the time and have controllers inside the heap of folders to organize my code. The easiest way is to first add the controller to the Controller folder. So he will have his own namespace. Then just drag the file to the folder you want to organize it into. 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 to the desired folders.

0
source

Assuming you are using MVC5, I would definitely consider using attribute-based routing in ASP.NET MVC as follows:

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 as follows:

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

If you need more information, read this blog post I wrote on this topic.

0
source

Source: https://habr.com/ru/post/1262946/


All Articles