Scott Baker
Scott Baker

Reputation: 10453

How can I relocate my MVC6 folders to another folder and still have them work

I'm using MVC6, and I can't figure out how to relocate these folders in my project:

  • MyWebApp
    • Controllers
    • ViewModels
    • Views

Into a common folder like this:

  • MyWebApp
    • Mvc
      • Controllers
      • ViewModels
      • Views

Physically relocating the files and folders is easy enough, but MVC can't find the files anymore:

InvalidOperationException: The view 'Index' was not found. The following locations were searched:

/Views/App/Index.cshtml 
/Views/Shared/Index.cshtml

How do I add the /Mvc folder as a location to look for MVC related goodness?

Upvotes: 2

Views: 104

Answers (1)

Reza
Reza

Reputation: 5634

What you are looking for is called Areas. Unlike in older versions of MVC, with Core there is no formal "Areas" option when you right click on the Project. You can however create a folder and name it "Areas". Within the Controllers you can decorate each controller with [Area("myArea")] to specify the controller for within the Areas.

In the startup.cs, setup is as follows:

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


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

Upvotes: 2

Related Questions