Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 2.1 Areas routing not working

I have this folder structure for my new Area

enter image description here

This is how I set it up in my startup:

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

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

This is how I created my basecontroller

namespace App.Areas.Applications.Controllers
{
    [Area("Applications")]
    [Authorize]
    public abstract class ApplicationsBaseController : Controller
    {

    }
}

My ApplicationsController then inherits the BaseController

However, when I set a link like this

<li class="nav-item"><a asp-area="Applications" asp-controller="Applications" asp-action="Index" class="nav-link">Applications</a></li>

This is the link that shows up in my url https://localhost:44338/Applications?area=Applications and I get a page cannot be found.

What did I miss when setting up my Area?

EDIT:

When I add [Route("Applications/[controller]")] after my [Area("Applications")], I get this error

An unhandled exception occurred while processing the request. AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

App.Areas.Applications.Controllers.ApplicationsController.Index (App) App.Areas.Applications.Controllers.ApplicationsController.Create (App) App.Areas.Applications.Controllers.ApplicationsController.NewRole (App)

like image 612
JianYA Avatar asked Jan 02 '23 19:01

JianYA


1 Answers

Put it before the default route... like this

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

routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}/{id?}");
});
like image 116
rykamol Avatar answered Jan 05 '23 16:01

rykamol