2016-08-12 192 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