2016-08-12 189 views
1

我要像下面的結構創建區域ASP.Net核心嵌套區

  • 地區
    • 聯繫
      • 前端
        • 控制器
          • 個HomeController.cs
        • 瀏覽
      • API
        • 控制器
          • HomeController.cs

啓動類

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
      loggerFactory.AddDebug(); 

      if (env.IsDevelopment()) 
      { 
       app.UseDeveloperExceptionPage(); 
       app.UseBrowserLink(); 
      } 
      else 
      { 
       app.UseExceptionHandler("/Home/Error"); 
      } 

      app.UseStaticFiles(); 

      app.UseMvc(routes => 
      { 
       routes.MapRoute(name: "areaRoute", 
        template: "{area:exists}/{controller=Home}/{action=Index}"); 

       routes.MapRoute(
        name: "default", 
        template: "{controller=Home}/{action=Index}/{id?}"); 
      }); 
     } 

我已經標記爲[區( 「管理/前端」),以HomeController的,但它不工作。它返回以下錯誤

處理請求時發生未處理的異常。

InvalidOperationException:未找到'關於'視圖。搜索到以下位置: /Areas/Admin/Views/Home/About.cshtml

我該怎麼辦?

項目

enter image description here

enter image description here

+0

請檢查http://stackoverflow.com/questions/36535511/how-to-use-area-for-asp-net-core?rq = 1 –

+0

當然還有https://docs.asp.net/en/latest/mvc/controllers/areas.html –

+0

您的截圖有拼寫錯誤「前端」 - >「ForntEnd」..它可能是 –

回答

1

您可以使用AreaViewLocationFormatsRazorViewEngineOptions,表示你想MVC尋找視圖的所有路徑。

services.Configure<RazorViewEngineOptions>(o => 
{ 
    o.AreaViewLocationFormats.Insert(0, "/Areas/{2}/FrontEnd/Views/Shared/{0}.cshtml"); 
    o.AreaViewLocationFormats.Insert(0, "/Areas/{2}/FrontEnd/Views/{1}/{0}.cshtml"); 
}); 

你可以閱讀什麼AreaViewLocationFormats詳細的文檔是在這裏:https://github.com/aspnet/Mvc/blob/1.0.0/src/Microsoft.AspNetCore.Mvc.Razor/RazorViewEngineOptions.cs#L92

而且你可以裝飾你的控制器只是[Area("Admin")]

+0

它的工作。謝謝 – Eagle