using System; using Microsoft.AspNetCore.Mvc; namespace Umbraco.Web.Common.Extensions { public static class ControllerExtensions { /// /// Return the controller name from the controller type /// /// /// internal static string GetControllerName(Type controllerType) { if (!controllerType.Name.EndsWith("Controller")) { throw new InvalidOperationException("The controller type " + controllerType + " does not follow conventions, MVC Controller class names must be suffixed with the term 'Controller'"); } return controllerType.Name.Substring(0, controllerType.Name.LastIndexOf("Controller")); } /// /// Return the controller name from the controller instance /// /// /// internal static string GetControllerName(this Controller controllerInstance) { return GetControllerName(controllerInstance.GetType()); } /// /// Return the controller name from the controller type /// /// /// /// internal static string GetControllerName() { return GetControllerName(typeof(T)); } } }