Search code examples
asp.net-mvc-routingasp.net-core-mvc

MVC6 View Referencing issue due to routing


In my solution I have at the root level a Controller, Views Folder each of these have a Home Folder with an Home Controller with Index method and the view folder with an index view (default MVC setup). At the root level I have introduced an Areas folder then in here I have created another Folder for the area then folders for controllers and views. Home controller with index method and views folder with index view. it all builds fine, but I receive the following error:

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

VisualJobs.Controllers.HomeController.Index (VisualJobs) VisualJobs.Areas.Recruiter.Controllers.HomeController.Index (VisualJobs) VisualJobs.Areas.Jobs.Controllers.HomeController.Index (VisualJobs)

in my Configuration file I have:

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

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

        });

Folder structure:

 Areas -->Candidate-->Controllers
                      Shared
                      ViewModels
                      Views 
 Areas -->Recruiter-->Controllers
                      Shared
                      ViewModels
                      Views 
Controllers 
Views

Solution

  • It may be because you have not decorated your area controller classes with the Area attribute. Something like this.

    [Area("Recruiter")]
    public class MyController : Controller
    {
        ...
    }
    

    You also need _ViewImports.cshtml and _ViewStart.cshtml in the Views folder of your area.