Subfolder in ASP.NET MVC Controllers

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 that the page was not 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 } ); } } } 
+2
source share
2 answers

You can use the following route to solve your problem:

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

DO NOT FORGET changing the default value for controller = "Home" to the controller where you want to redirect when the user types http://localhost:port/Admin/ .

So, when you go to http://localhost:port/Admin/Login/ , you will use the Login controller and Index action in the admin folder.

IMPORTANT Also put this route before the default route, because if you put this code after the "default" route, ASP.NET will read your http://localhost:port/Admin/Login/ as a URL with the Admin and Login controller .

+4
source

Your new โ€œSubFolderโ€ route does not include the ability to enable actions on the route (in your case, โ€œAdminโ€).

Your url wants to map a route, e.g.

 "SubFolder/ChildController/{action}" 

If you didnโ€™t indicate "{action}" on your route, it will not match your route. Then he tries to use the default route, which obviously does not work.

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

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

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


All Articles