2016-11-11 72 views
1

我在我的MVC項目中使用區域有一些問題。我能夠訪問我的控制器,這是位於下方的區域,但是當它返回到視圖(模型),我得到一個錯誤:MVC區域 - 控制器不返回視圖

The view 'Index' or its master was not found or no view engine supports the searched locations.The following locations were searched: ~/Views/MyController/Index.aspx ~/Views/MyController/Index.ascx etc.

這裏是MyAreaAreaRegistration:

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

Routeconfig:

routes.MapRoute(
       name: "Default", // Route name 
       url: "{controller}/{action}/{id}", // URL with parameters 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
       namespaces: new[] { "MyApp.Controllers" } 
      ); 

的Global.asax:

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

和控制器:

return View(myViewModel); 

我完全堅持這一之一。任何幫助將非常感激。

回答

2

請更改您的路由配置代碼。

routes.MapRoute(
       name: "Default", // Route name 
      url: "{controller}/{action}/{id}", // URL with parameters 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ).DataTokens.Add("area", "MyArea"); ; 

這將是爲你工作。

謝謝。

+0

嗨,這確實適用於這方面的觀點,但我也有意見和控制器以外的地區(「主要應用程序」),並且這些停止工作。如果我更改爲DataTokens.Add(「區域」,「」),則其他人工作,但區域不工作。 – punatiainen

相關問題