Area Controller的URL太长,如何缩短URL?
Controller's URL in Area is too long, how to shorten URL?
我不熟悉 MVC 中的区域术语,我想使用它。我有以下目录指向区域中的控制器。
Areas > Admin > Controllers > AdminController
当我想访问AdminController的Index Action时,只需要访问http://localhost/Admin/Admin. I want to get rid of the second "Admin". I want to type http://localhost/Admin/即可。我该怎么做?
AdminAreaRegistration.cs 具有以下 MapRoute
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
您没有为控制器指定默认值,因此除非您将其包含在 url 中,否则路由引擎无法识别您要导航到哪个控制器。您可以通过为控制器名称提供默认值来解决此问题
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
虽然这意味着 ../Admin
将导航到 AdminController
的 Index()
方法,但这仍然意味着如果您想导航到 AdminController
中的另一个方法,您的 url 需要 ../Admin/Admin/AnotherMethod
,这可能不是您想要的。
区域的目的是对您的控制器和方法进行逻辑分组。例如,购物车应用程序可能有一个 ProductsController
,用户可能会导航到 ../Product
以显示产品列表或 ../Product/Details/1
以显示产品的详细信息。但是应用程序可能需要其他方法供供应商创建和编辑他们的产品,因此您可以创建一个单独的 Suppliers
区域,../Suppliers/Products
将导航到他们的产品列表,../Suppliers/Products/Edit/1
将允许他们更新他们产品的详细信息。
在 Admin
区域中设置 AdminController
没有任何意义,我建议它应该是 HomeController
如果它包含与管理任务相关的通用方法(以及路由定义将是 new { controller = "Home", .. }
我不熟悉 MVC 中的区域术语,我想使用它。我有以下目录指向区域中的控制器。
Areas > Admin > Controllers > AdminController
当我想访问AdminController的Index Action时,只需要访问http://localhost/Admin/Admin. I want to get rid of the second "Admin". I want to type http://localhost/Admin/即可。我该怎么做?
AdminAreaRegistration.cs 具有以下 MapRoute
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
您没有为控制器指定默认值,因此除非您将其包含在 url 中,否则路由引擎无法识别您要导航到哪个控制器。您可以通过为控制器名称提供默认值来解决此问题
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
虽然这意味着 ../Admin
将导航到 AdminController
的 Index()
方法,但这仍然意味着如果您想导航到 AdminController
中的另一个方法,您的 url 需要 ../Admin/Admin/AnotherMethod
,这可能不是您想要的。
区域的目的是对您的控制器和方法进行逻辑分组。例如,购物车应用程序可能有一个 ProductsController
,用户可能会导航到 ../Product
以显示产品列表或 ../Product/Details/1
以显示产品的详细信息。但是应用程序可能需要其他方法供供应商创建和编辑他们的产品,因此您可以创建一个单独的 Suppliers
区域,../Suppliers/Products
将导航到他们的产品列表,../Suppliers/Products/Edit/1
将允许他们更新他们产品的详细信息。
在 Admin
区域中设置 AdminController
没有任何意义,我建议它应该是 HomeController
如果它包含与管理任务相关的通用方法(以及路由定义将是 new { controller = "Home", .. }