Asp.net mvc 3 无法理解Asp.net MVC路由

Asp.net mvc 3 无法理解Asp.net MVC路由,asp.net-mvc-3,routing,url-routing,Asp.net Mvc 3,Routing,Url Routing,我已将以下路径添加到我的global.asax文件中:- routes.MapRoute( "Admin_Route", "Admin/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional }, new[] { "PriceCompare.Admin.Controllers" } ); routes.MapRoute( "Default",

我已将以下路径添加到我的global.asax文件中:-

routes.MapRoute(
    "Admin_Route",
    "Admin/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    new[] { "PriceCompare.Admin.Controllers" }
    );

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
    new[] { "PriceCompare.Controllers" } 
    );
管理控制器(即ManageCatsController、ManageBrandsController等)位于PriceCompare.admin.Controller命名空间中,其他常规控制器位于PriceCompare.Controller命名空间中

问题是,我可以通过在所有控制器前面添加Admin/来访问它们,而不管它们是否位于PriceCompare.Admin.controllers命名空间中

此外,我可以直接访问管理员控制器,而无需前缀admin/


为什么会发生这种情况。我是否误解了路由行为

您需要注册您的管理区域

当我为区域注册路线时,我总是这样做:

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

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new string[] { "AppName.Controllers" }
        );
 }
在App/Areas/Admin文件夹内创建一个AdminAreaRegistration.cs文件,其中包含此文件

using System.Web.Mvc;
namespace AppName.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin",
                "Admin/{controller}/{action}/{id}",
                new { controller="Home", action = "Index", id = UrlParameter.Optional },
                new string[] { "AppName.Areas.Admin.Controllers" }
            );
        }
    }
}
现在在
Global.asax中执行此操作:

protected void Application_Start()
    {
        // Add this next line
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        // Add any other stuff (like IoC or whatever)
    }
并且只在
Global.asax
中的
RegisterRoutes
中注册您的正常路由,如下所示:

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

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new string[] { "AppName.Controllers" }
        );
 }
我还建议将
区域
部分保留在您的管理名称空间中(在您的情况下称之为
PriceCompare.Areas.Admin.Controller
),因为这将使以后的工作更加轻松


请尝试一下,并让我知道它是否有效:-)

您需要注册您的管理区域

当我为区域注册路线时,我总是这样做:

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

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new string[] { "AppName.Controllers" }
        );
 }
在App/Areas/Admin文件夹内创建一个AdminAreaRegistration.cs文件,其中包含此文件

using System.Web.Mvc;
namespace AppName.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin",
                "Admin/{controller}/{action}/{id}",
                new { controller="Home", action = "Index", id = UrlParameter.Optional },
                new string[] { "AppName.Areas.Admin.Controllers" }
            );
        }
    }
}
现在在
Global.asax中执行此操作:

protected void Application_Start()
    {
        // Add this next line
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        // Add any other stuff (like IoC or whatever)
    }
并且只在
Global.asax
中的
RegisterRoutes
中注册您的正常路由,如下所示:

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

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new string[] { "AppName.Controllers" }
        );
 }
我还建议将
区域
部分保留在您的管理名称空间中(在您的情况下称之为
PriceCompare.Areas.Admin.Controller
),因为这将使以后的工作更加轻松


请尝试一下,并让我知道它是否有效:-)

我将不得不改变很多:一个全新的层次结构。我在一个新的mvc应用程序中尝试了
区域
概念,它按照我需要的方式工作。事实上,我有点困惑于理解为什么我当前的路由行为不正常。为什么我可以通过后缀“Admin/”访问所有controllerx。在区域概念中,我想也应该使用名称空间。如果您指定的是一个包含控制器
PriceCompare.Admin.controllers
而不是
PriceCompare.controllers
的整个其他名称空间,那么这听起来确实像是在使用定义的单独的
区域。不要太灰心;我不得不对一个项目做一个类似的改变,没花那么长时间。只需确保在开始之前备份/提交任何内容即可。我很高兴代码在任何情况下都能工作。祝你好运!:-)我将不得不改变很多:一个全新的层次结构。我在一个新的mvc应用程序中尝试了
区域
概念,它按照我需要的方式工作。事实上,我有点困惑于理解为什么我当前的路由行为不正常。为什么我可以通过后缀“Admin/”访问所有controllerx。在区域概念中,我想也应该使用名称空间。如果您指定的是一个包含控制器
PriceCompare.Admin.controllers
而不是
PriceCompare.controllers
的整个其他名称空间,那么这听起来确实像是在使用定义的单独的
区域。不要太灰心;我不得不对一个项目做一个类似的改变,没花那么长时间。只需确保在开始之前备份/提交任何内容即可。我很高兴代码在任何情况下都能工作。祝你好运!:-)