using Microsoft.AspNetCore.Mvc.ApplicationModels; using System.Linq; namespace AppModelSample.Conventions { public class NamespaceRoutingConvention : IApplicationModelConvention { public void Apply(ApplicationModel application) { foreach (var controller in application.Controllers) { var hasAttributeRouteModels = controller.Selectors .Any(selector => selector.AttributeRouteModel != null); if (!hasAttributeRouteModels && controller.ControllerName.Contains("Namespace")) // affect one controller in this sample { // Replace the . in the namespace with a / to create the attribute route // Ex: MySite.Admin namespace will correspond to MySite/Admin attribute route // Then attach [controller], [action] and optional {id?} token. // [Controller] and [action] is replaced with the controller and action // name to generate the final template controller.Selectors[0].AttributeRouteModel = new AttributeRouteModel() { Template = controller.ControllerType.Namespace.Replace('.', '/') + "/[controller]/[action]/{id?}" }; } } // You can continue to put attribute route templates for the controller actions depending on the way you want them to behave } } }