using System; using System.Collections.Generic; using System.Web.Mvc; using System.Web.Routing; using Umbraco.Core; using Umbraco.Core.Configuration; namespace Umbraco.Web.Mvc { internal static class AreaRegistrationExtensions { /// /// Creates a custom individual route for the specified controller plugin. Individual routes /// are required by controller plugins to map to a unique URL based on ID. /// /// /// /// An existing route collection /// /// The suffix name that the controller name must end in before the "Controller" string for example: /// ContentTreeController has a controllerSuffixName of "Tree", this is used for route constraints. /// /// /// /// /// The DataToken value to set for the 'umbraco' key, this defaults to 'backoffice' /// /// internal static void RouteControllerPlugin(this AreaRegistration area, string controllerName, Type controllerType, RouteCollection routes, string controllerSuffixName, string defaultAction, object defaultId, string umbracoTokenValue = "backoffice") { Mandate.ParameterNotNullOrEmpty(controllerName, "controllerName"); Mandate.ParameterNotNullOrEmpty(controllerSuffixName, "controllerSuffixName"); Mandate.ParameterNotNullOrEmpty(defaultAction, "defaultAction"); Mandate.ParameterNotNull(controllerType, "controllerType"); Mandate.ParameterNotNull(routes, "routes"); Mandate.ParameterNotNull(defaultId, "defaultId"); var umbracoArea = GlobalSettings.UmbracoMvcArea; //routes are explicitly name with controller names and IDs var url = umbracoArea + "/" + area.AreaName + "/" + controllerName + "/{action}/{id}"; //create a new route with custom name, specified url, and the namespace of the controller plugin var controllerPluginRoute = routes.MapRoute( //name string.Format("umbraco-{0}", controllerType.FullName), //url format url, //set the namespace of the controller to match new[] { controllerType.Namespace }); //set defaults controllerPluginRoute.Defaults = new RouteValueDictionary( new Dictionary { { "controller", controllerName }, { "action", defaultAction }, { "id", defaultId } }); //constraints: only match controllers ending with 'controllerSuffixName' and only match this controller's ID for this route controllerPluginRoute.Constraints = new RouteValueDictionary( new Dictionary { { "controller", @"(\w+)" + controllerSuffixName } }); //match this area controllerPluginRoute.DataTokens.Add("area", area.AreaName); controllerPluginRoute.DataTokens.Add("umbraco", umbracoTokenValue); //ensure the umbraco token is set } } }