2017-06-20 67 views
0

我有一種情況,在ASP.NET MVC 5應用程序的管理「區域」中有幾個控制器。路由不能一致地爲ASP.NET區域頁面工作

其中一個控制器工作正常,第二個控制器路由到站點根目錄下的默認路由。

我的默認路由:

routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Reports", action = "Index", id = UrlParameter.Optional } 
     ); 

我區路線:

public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.MapRoute(
      "Administration_default", 
      "Administration/{controller}/{action}/{id}", 
      new { action = "Index", id = UrlParameter.Optional } 
     ); 
    } 

我的工作區域控制器:

public class ResourcesController : Controller 
{ 
    // GET: Administration/Resources 
    public ActionResult Index() 
    { 
     using (var db = new SDRContext()) 
     { 
      var resource = db.Resources.ToList(); 
      return View(resource); 
     } 
    } 
} 

我的非工作區域控制器:

public class DisplayFieldsController : Controller 
{ 
    private SDRContext db = new SDRContext(); 

    // GET: Administration/DisplayFields 
    public ActionResult Index() 
    { 
     var results = db.DisplayFields.ToList(); 
     return View(results); 
    } 
} 

爲什麼當我打電話第二控制器它默認的根路徑,而不是給藥途徑我已經安裝?

回答

0

我假設您的DisplayFields控制器與第一個存在於相同的區域文件夾中。你怎麼調用你的第二個控制器?你是從鏈接做到這一點?我假設你的區域被稱爲管理:

Html.ActionLink("Link Text", "Index", "DisplayFields", new { Area = 
"Adminstration" },null); 
0

爲什麼當我打電話第二控制器它默認的根路徑,而不是給藥途徑我已經安裝?

號碼路由利用按照它們註冊的順序,句點。我懷疑如果你有問題,你沒有在應用程序啓動時按正確的順序設置你的路由。正確的順序是註冊您的區域路線之前您的默認路線。

AreaRegistration.RegisterAllAreas(); 
// ... 
RouteConfig.RegisterRoutes(RouteTable.Routes); 

如果使用屬性的路由,應該註冊它之前您所在地區的航線。