Attribute Routing not working in areas

You are probably combining convention based routing with attribute routing, and you should register your areas after you map the attribute routes.

The line

AreaRegistration.RegisterAllAreas();

should be called AFTER this line:

routes.MapMvcAttributeRoutes();

The explanation (from https://devblogs.microsoft.com/aspnet/attribute-routing-in-asp-net-mvc-5/):

If you are using both Areas with route attributes, and areas with convention based routes (set by an AreaRegistration class), then you need to make sure that area registration happen after MVC attribute routes are configured, however before the default convention-based route is set. The reason is that route registration should be ordered from the most specific (attributes) through more general (area registration) to the mist generic (the default route) to avoid generic routes from “hiding” more specific routes by matching incoming requests too early in the pipeline.

When you create a blank asp.net mvc website, add an area and start using attribute routing, you will encounter this problem because the “Add Area” action in visual studio adds the RegisterAllAreas call in your Application_Start, before the route configuration..

Alternative solution

Perhaps you do not intend to keep using convention based routing and prefer to only use attribute routing.
In this case you can just delete the FormsAreaRegistration.cs file.

Leave a Comment