我可以拥有与区域同名的控制器吗?
Can I have a controller with the same name as an area?
假设我的 MVC 应用程序中有一个名为 Admin
的区域。该区域有一个 HomeController
和一个 Index
动作,如下所示:
using System.Web.Mvc;
namespace AreasPractice.Areas.Admin.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return Content("Admin.HomeController");
}
}
}
管理区域的区域注册具有以下区域相关路由的默认值:
using System.Web.Mvc;
namespace AreasPractice.Areas.Admin
{
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//, new[] { "AreasPractice.Areas.Admin.Controllers" }
);
}
}
}
我的应用程序的根区域也有一个 HomeController
,带有 Index
操作和路由配置,如下所示:
using System.Web.Mvc;
namespace AreasPractice.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return Content("From home controller of the root");
}
}
}
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 }
, namespaces: new[] { "AreasPractice.Controllers" }
);
}
}
现在,我在我的应用程序的根区域添加一个带有 Index
操作的 AdminController
,如下所示:
using System.Web.Mvc;
namespace AreasPractice.Controllers
{
public class AdminController : Controller
{
public ActionResult Index()
{
return Content("Root -> Admin Controller -> Index action.");
}
}
}
当我运行申请时,我期待看到一些有趣的东西,比如,可能是一个例外的效果,"I can't figure out what route you want."
但是当我 运行 应用程序时,它 运行 很好并且请求:
/Admin/
屈服于 Admin
地区的 HomeController
。
这是因为,显然,我记得,路由机制是按优先级工作的。它找到 第一个匹配项 并随之匹配。
并且显然发现 Admin_default 路由甚至在应用 Default 路由之前就满足了请求模式。
我的问题:
到目前为止我的理解是否正确?
我该怎么玩呢?如果我想让它去
AdminController 在应用程序的根区域?
好吧,我想了想得到了答案。如果我只是在 global.asax 文件中将区域注册移动到 之后 默认路由已注册,它现在转到我根的 AdminController
而不是去前往 Admin
区域的 HomeController
.
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// Remove from here
// AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// Put here after the default route has been
// registered via the RouteConfig.RegisterRoutes
// call above
AreaRegistration.RegisterAllAreas();
}
}
假设我的 MVC 应用程序中有一个名为 Admin
的区域。该区域有一个 HomeController
和一个 Index
动作,如下所示:
using System.Web.Mvc;
namespace AreasPractice.Areas.Admin.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return Content("Admin.HomeController");
}
}
}
管理区域的区域注册具有以下区域相关路由的默认值:
using System.Web.Mvc;
namespace AreasPractice.Areas.Admin
{
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//, new[] { "AreasPractice.Areas.Admin.Controllers" }
);
}
}
}
我的应用程序的根区域也有一个 HomeController
,带有 Index
操作和路由配置,如下所示:
using System.Web.Mvc;
namespace AreasPractice.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return Content("From home controller of the root");
}
}
}
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 }
, namespaces: new[] { "AreasPractice.Controllers" }
);
}
}
现在,我在我的应用程序的根区域添加一个带有 Index
操作的 AdminController
,如下所示:
using System.Web.Mvc;
namespace AreasPractice.Controllers
{
public class AdminController : Controller
{
public ActionResult Index()
{
return Content("Root -> Admin Controller -> Index action.");
}
}
}
当我运行申请时,我期待看到一些有趣的东西,比如,可能是一个例外的效果,"I can't figure out what route you want."
但是当我 运行 应用程序时,它 运行 很好并且请求:
/Admin/
屈服于 Admin
地区的 HomeController
。
这是因为,显然,我记得,路由机制是按优先级工作的。它找到 第一个匹配项 并随之匹配。
并且显然发现 Admin_default 路由甚至在应用 Default 路由之前就满足了请求模式。
我的问题:
到目前为止我的理解是否正确?
我该怎么玩呢?如果我想让它去 AdminController 在应用程序的根区域?
好吧,我想了想得到了答案。如果我只是在 global.asax 文件中将区域注册移动到 之后 默认路由已注册,它现在转到我根的 AdminController
而不是去前往 Admin
区域的 HomeController
.
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// Remove from here
// AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// Put here after the default route has been
// registered via the RouteConfig.RegisterRoutes
// call above
AreaRegistration.RegisterAllAreas();
}
}