title: ASP.Net Core Area Routing without AreaAttribute with default routes
body: <p>I have a problem regarding AreaRouting in my ASP.Net Core MVC project. I want to use Area routing without adding AreaAttribute for each controller (I move from an existing ASP.NET MVC 5 project with hundreds of controller and adding this attribute is really cumbersome)</p>
  <p>My sample application folder structure looks like this:
  <a href="https://i.stack.imgur.com/W07Ur.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W07Ur.png" alt="enter image description here" /></a></p>
  <p>The namespaces follow the folder structure.
  I found a way to add a custom convention and extract the area name:</p>
  <pre><code>internal sealed class AreaRoutingConvention : IControllerModelConvention
      {
          public void Apply(ControllerModel controller)
          {
              var hasRouteAttributes = controller.Selectors.Any(selector =&gt; selector.AttributeRouteModel != null);
  
              if (hasRouteAttributes)
              {
                  return;
              }
  
              var controllerTypeNamespace = controller.ControllerType.Namespace;
              if (controllerTypeNamespace == null)
                  return;
  
              var areaName = controllerTypeNamespace.Split('.').SkipWhile(segment =&gt; segment != &quot;Areas&quot;).Skip(1).FirstOrDefault();
  
              
              if (!string.IsNullOrEmpty(areaName))
              {
                  var template = areaName + &quot;/[controller]/[action]/{id?}&quot;;
                  controller.RouteValues.Add(&quot;area&quot;, areaName);
  
                  foreach (var selector in controller.Selectors)
                  {
                      selector.AttributeRouteModel = new AttributeRouteModel
                      {
                          Template = template
                      };
                  }
              }
          }
      }
  </code></pre>
  <p>My sample route configuration is this one:</p>
  <pre><code>app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapAreaControllerRoute(
                      name: &quot;Activity_default&quot;,
                      areaName: &quot;Activity&quot;,
                      &quot;Activity/{controller=Activity}/{action=Index}/{id?}&quot;,
                      defaults: new { controller = &quot;Activity&quot;, action = &quot;Index&quot; }
                  );
                  
                  endpoints.MapAreaControllerRoute(
                      name: &quot;Admin_default&quot;,
                      areaName: &quot;Admin&quot;,
                      &quot;Admin/{controller=Admin}/{action=Index}/{id?}&quot;,
                      defaults: new { controller = &quot;Admin&quot;, action = &quot;Index&quot; }
                  );
  
                  endpoints.MapAreaControllerRoute(
                      name: &quot;home_default&quot;,
                      areaName: &quot;Home&quot;,
                      pattern: &quot;{area}/{controller=Home}/{action=Index}/{id?}&quot;);
              });
  </code></pre>
  <p>It works in almost all of my cases:
  Now when I enter</p>
  <ul>
  <li>/Activity/Activity/Index -&gt; Index method is invoked</li>
  <li>/Activity/Home/Index -&gt; Not found, as intended</li>
  <li>/Home/Home/Index -&gt; Index method is invoked</li>
  <li>/Home/Activity/Index -&gt; Not found, as intended</li>
  <li>/Admin/Admin/Index -&gt; Index method is invoked</li>
  <li>/Admin/Activity/Index -&gt; Not found, as intended</li>
  </ul>
  <p>But I also want to route to the /Activity/Activity/Index method when the user enters</p>
  <ul>
  <li>/Activity</li>
  <li>/Activity/Activit/</li>
  </ul>
  <p>Can anyone please help with this problem? When I do not use my custom convention and add Area-Attributes to the controllers all works well.</p>