using Microsoft.AspNetCore.Mvc.ApplicationModels; using System; namespace AppPartsSample { // Used to set the controller name for routing purposes. Without this convention the // names would be like 'GenericController`1[Widget]' instead of 'Widget'. // // Conventions can be applied as attributes or added to MvcOptions.Conventions. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] public class GenericControllerNameConvention : Attribute, IControllerModelConvention { public void Apply(ControllerModel controller) { if (controller.ControllerType.GetGenericTypeDefinition() != typeof(GenericController<>)) { // Not a GenericController, ignore. return; } var entityType = controller.ControllerType.GenericTypeArguments[0]; controller.ControllerName = entityType.Name; } } }