[英]Controller's URL in Area is too long, how to shorten URL?
我是MVC中Area術語的新手,我想使用它。 我有以下目錄指向Area中的控制器。
Areas > Admin > Controllers > AdminController
當我想訪問AdminController的Index Action時,我需要訪問http:// localhost / Admin / Admin 。 我想擺脫第二個“管理員”。 我只想輸入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
另一個方法,則您的網址將需要為../Admin/Admin/AnotherMethod
,即可能不是您想要的。
區域的目的是對控制器和方法進行邏輯分組。 例如,一個購物車的應用程序可能有一個ProductsController
,用戶可以瀏覽到../Product
顯示的產品或列表../Product/Details/1
顯示產品的詳細信息。 但是該應用可能需要供供應商使用其他方法來創建和編輯其產品,因此您將創建一個單獨的“ Suppliers
區域, ../Suppliers/Products
Suppliers
../Suppliers/Products
將導航至其產品列表, ../Suppliers/Products/Edit/1
將允許他們更新其產品的詳細信息。
在Admin
區域中放置AdminController
並沒有多大意義,我建議如果它包含與管理任務相關聯的常規方法,則應使用HomeController
(然后路由定義將是new { controller = "Home", .. }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.