3

I want to create area like the following structure

  • Areas
    • Admin
      • FrontEnd
        • Controllers
          • HomeController.cs
        • Views
      • API
        • Controllers
          • HomeController.cs

Startup class

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?}");
            });
        }

I already marked [Area("Admin/FrontEnd")] to HomeController but it doesn't work. It return the following error

An unhandled exception occurred while processing the request.

InvalidOperationException: The view 'About' was not found. The following locations were searched: /Areas/Admin/Views/Home/About.cshtml

How can I do?

Project

enter image description here

enter image description here

6
  • Please check stackoverflow.com/questions/36535511/… Commented Aug 12, 2016 at 3:29
  • and of course docs.asp.net/en/latest/mvc/controllers/areas.html Commented Aug 12, 2016 at 3:30
  • 1
    Your screenshot have misspeled "FrontEnd" -> "ForntEnd".. it could be that Commented Aug 12, 2016 at 3:31
  • Already changed but still not work. You provider link is not for nested area. I found the link (github.com/aspnet/Mvc/issues/1595) that it mention ASP.net core doesn't support nested area. is it really? Commented Aug 12, 2016 at 3:41
  • Ouch... Still you can do one thing. Put Controllers wherever you want (they match by class name, not by namespace). Put Views wherever you want but on each method return View("~/Full/Relative/Path/About.cshtml"); Commented Aug 12, 2016 at 3:43

1 Answer 1

3

You can use the AreaViewLocationFormats on RazorViewEngineOptions to indicate what all paths you want MVC to look for views.

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");
});

You can read the detailed documentation on what AreaViewLocationFormats is over here: https://github.com/aspnet/Mvc/blob/1.0.0/src/Microsoft.AspNetCore.Mvc.Razor/RazorViewEngineOptions.cs#L92

Also you can just decorate your controllers to be just [Area("Admin")]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.