2015-12-22 36 views
0

在我的應用程序中,我有兩個區域:PortalAdmin。我想在XXXAreaRegistration.cs文件中配置路由。使用前綴或不使用區域的路由

我想要的是/{controller}/{action}Portal/admin/{controller}/{action}Admin面板。

我想下面的配置,使之成爲可能:

// AdminAreaRegistration.cs 
context.MapRoute("Admin", 
       "admin/{controller}/{action}", 
       new { action = "Index", controller = "Admin" }); 

// PortalAreaRegistration.cs 
context.MapRoute("Portal", 
       "{controller}/{action}", 
       new { action = "Index", controller = "Portal" }); 

對於/我得到我的Portal/Index.cshtml,但/admin我得到404 ......我在它Portal區域查找AdminController第二種情況假設這就是爲什麼我得到404。但是如何制定一個解決方法來創建這樣的路由?

回答

1

要解決,你現在有一個約束添加到您的門戶區登記的路由碰撞:

// PortalAreaRegistration.cs 
context.MapRoute(
    "Portal", 
    "{controller}/{action}", 
    new { action = "Index", controller = "Portal" }, 
    new { controller = "^(?!.*admin).*$" } 
); 

這將確保,因爲你要通過這個服務的門戶區域將被映射到一切{controller}/{action}除了admin/*管理區域。

當然,由於更爲明顯的原因,您的Portal區域中不能有一個名爲AdminController的控制器。

+0

工程就像一個魅力:)謝謝你,先生! – Nickon

1
在你的代碼

當你調用http://www.domain.com/admin/你的路由搜索爲http://www.domain.com/admin/admin因爲你的默認控制器是

我想有一個區域沒有控制研究,例如管理

UPTADE管理員設定的默認參數爲現有的控制FOR名字空間

修復這樣的代碼以低於

context.MapRoute("Admin", 
      "admin/{controller}/{action}", 
      new { action = "Index", controller="Home"}, 
      new string[] { "MyApp.Admin.Controllers" } // specify the new namespace);