5

我有這樣的路線:Asp.net MVC 3路由區失敗

WebSite/Global.asax.cs我的網站路線:

namespace WebSite 
{ 
    public class MvcApplication : HttpApplication 
    { 
     public static void RegisterRoutes(RouteCollection routes) { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
      ... 
      routes.MapRoute(
       "Default", 
       "Authenticated/{controller}/{action}/{id}", 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
       new[] { "WebSite.Controllers" } 
      );  
      ... 
     } 

     void Application_Start() 
     { 
      ... 
      AreaRegistration.RegisterAllAreas(); 
      RegisterRoutes(RouteTable.Routes); 
      ... 
     }  
    } 
} 

WebSite/Areas/Admin/AdminAreaRegistration.cs我的管理區的路線:

namespace WebSite.Areas.Admin 
{ 
    public class AdminAreaRegistration : AreaRegistration 
    { 
     public override string AreaName 
     { 
      get 
      { 
       return "Admin"; 
      } 
     } 

     public override void RegisterArea(AreaRegistrationContext context) 
     { 
      context.MapRoute(
       "Admin_default", 
       "qwerty/Admin/{controller}/{action}/{id}", 
       new { action = "Index", id = UrlParameter.Optional }, 
       new[] { "WebSite.Areas.Admin.Controllers" } 
      ); 
     } 
    } 
} 

我的網址:

WebSite: http://www.mywebsite.com/Authenticated/Controller/Action... 
Admin: http://www.mywebsite.com/qwerty/Admin/Controller/Action... 

我的問題:

使用WebSite URL,我可以在不使用「qwerty/Admin」的情況下從Admin Area調用Controllers/Actions,這是不對的。我怎樣才能解決這個問題?

謝謝。

+0

可能是您的管理路由不適合您的控制器操作,因爲您沒有在路由中定義控制器。我的意思是「controller =」Home「語句 –

+0

@ Andrey.Gubal如果我在網站和管理區域中有兩個同名的控制器,則會出現同樣的問題,通過這種方式我將命名空間放在路由中,但沒有解決任何問題 – Cesar

回答

3

只需在每個MapRoute之後放置此代碼即可。它應該工作!

.DataTokens["UseNamespaceFallback"] = false; 
+1

謝謝,這個作品:D – Cesar