2016-12-14 80 views
0

正如標題所示,我遇到了路由和區域問題。我的項目中有一個區域部分,其中只有一個區域。它在那裏有一個策略控制器。在項目外部有一個帶有LoginController的控制器文件夾。當我嘗試訪問URL「BoB/Login/ChangeSection」(BoB是項目名稱,登錄是控制器,ChangeSection是動作)時,它會被髮送到「BoB/Policy/Login」。這意味着策略區域內的區域註冊會抓取請求並將策略前綴放在前面。與區域相關的路由問題

這裏是我的項目RouteConfig

routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "BoBHome", action = "Index", id = UrlParameter.Optional }, 
      namespaces: new string[] { "BoB.Controllers" } 
      ); 

,這裏是區塊登記

public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.MapRoute(
      name: "BoBPolicy_default", 
      url: "Policy/{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      namespaces: new string[] { "BoB.Areas.BoBPolicy.Controllers" } 
     ); 
    } 

在我印象中的命名空間會繼續發生這種情況下,但它顯然不是。任何幫助表示讚賞。

一些額外的信息:

這是來自另一個項目,是不是MVC所以重定向看起來是這樣的:

Response.Redirect("/BoB/BoBLogin/ChangeSection?controller=PolicyOverview"); 

回答

1

我覺得你的問題是類似non area route issue,這需要區域參數的用法防止在使用默認路由時插入區域URL前綴:

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    // set area parameter with area name 
    context.MapRoute(
     name: "BoBPolicy_default", 
     url: "Policy/{controller}/{action}/{id}", 
     // add area parameter here 
     defaults: new { area = "Policy", controller = "Home", action = "Index", id = UrlParameter.Optional }, 
     namespaces: new string[] { "BoB.Areas.BoBPolicy.Controllers" } 
    ); 
} 

注意:由於區域路由註冊總是放在def (由於原因,請參閱this answer),當區域路由約束未指定時,路由引擎可能會錯誤地將默認路由請求提取到區域路由中。

相關:

MVC5 Routing Go to specific Areas view when root url of site is entered