Subfolder in Controllers ASP.NET MVC [duplicate]

眉间皱痕 提交于 2020-01-21 08:35:31

问题


In my Controllers folder i want to have a subfolder called Admin.

When i go to http://localhost:port/Admin/Login/ it says the page could not be 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 }
            );
        }
    }
}

回答1:


You could use the next route to handle your issue:

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

DON'T FORGET to change default value for controller = "Home" to controller where you want to redirect when user types http://localhost:port/Admin/.

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

IMPORTANT Also put this route BEFORE default route, because if you put this code after your "Default" route ASP.NET will read your http://localhost:port/Admin/Login/ like URL with Admin controller and Login action.




回答2:


Your new route "SubFolder" does not include the possibility of including an action in the route (in your case, "Admin").

Your url wants to match routie like

"SubFolder/ChildController/{action}"

If don't include the "{action}" in your route, it won't match your route. It then tries the default route, which obviously fails.

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

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


来源:https://stackoverflow.com/questions/33802430/subfolder-in-controllers-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!