/// <summary> /// Auto-routes all back office api controllers /// </summary> private void AutoRouteBackOfficeApiControllers(IEndpointRouteBuilder endpoints) { // TODO: We could investigate dynamically routing plugin controllers so we don't have to eagerly type scan for them, // it would probably work well, see https://www.strathweb.com/2019/08/dynamic-controller-routing-in-asp-net-core-3-0/ // will probably be what we use for front-end routing too. BTW the orig article about migrating from IRouter to endpoint // routing for things like a CMS is here https://github.com/dotnet/aspnetcore/issues/4221 foreach (Type controller in _apiControllers) { PluginControllerMetadata meta = PluginController.GetMetadata(controller); // exclude front-end api controllers if (!meta.IsBackOffice) { continue; } endpoints.MapUmbracoApiRoute( meta.ControllerType, _umbracoPathSegment, meta.AreaName, meta.IsBackOffice, string.Empty); // no default action (this is what we had before) } }
/// <summary> /// Return the Url for an Umbraco controller /// </summary> public static string GetUmbracoControllerUrl(this LinkGenerator linkGenerator, string actionName, Type controllerType, IDictionary <string, object> values = null) { if (actionName == null) { throw new ArgumentNullException(nameof(actionName)); } if (string.IsNullOrWhiteSpace(actionName)) { throw new ArgumentException("Value can't be empty or consist only of white-space characters.", nameof(actionName)); } if (controllerType == null) { throw new ArgumentNullException(nameof(controllerType)); } var area = string.Empty; if (!typeof(ControllerBase).IsAssignableFrom(controllerType)) { throw new InvalidOperationException($"The controller {controllerType} is of type {typeof(ControllerBase)}"); } PluginControllerMetadata metaData = PluginController.GetMetadata(controllerType); if (metaData.AreaName.IsNullOrWhiteSpace() == false) { // set the area to the plugin area area = metaData.AreaName; } return(linkGenerator.GetUmbracoControllerUrl(actionName, ControllerExtensions.GetControllerName(controllerType), area, values)); }
/// <summary> /// Helper method to create a new form to execute in the Umbraco request pipeline to a surface controller plugin /// </summary> public static MvcForm BeginUmbracoForm( this IHtmlHelper html, string action, Type surfaceType, object?additionalRouteVals, IDictionary <string, object?> htmlAttributes, FormMethod method) { if (action == null) { throw new ArgumentNullException(nameof(action)); } if (string.IsNullOrWhiteSpace(action)) { throw new ArgumentException( "Value can't be empty or consist only of white-space characters.", nameof(action)); } if (surfaceType == null) { throw new ArgumentNullException(nameof(surfaceType)); } SurfaceControllerTypeCollection surfaceControllerTypeCollection = GetRequiredService <SurfaceControllerTypeCollection>(html); Type?surfaceController = surfaceControllerTypeCollection.SingleOrDefault(x => x == surfaceType); if (surfaceController == null) { throw new InvalidOperationException("Could not find the surface controller of type " + surfaceType.FullName); } PluginControllerMetadata metaData = PluginController.GetMetadata(surfaceController); var area = string.Empty; if (metaData.AreaName.IsNullOrWhiteSpace() == false) { // Set the area to the plugin area area = metaData.AreaName; } return(html.BeginUmbracoForm( action, metaData.ControllerName, area !, additionalRouteVals, htmlAttributes, method)); }
/// <summary> /// Auto-routes all front-end surface controllers /// </summary> private void AutoRouteSurfaceControllers(IEndpointRouteBuilder endpoints) { foreach (Type controller in _surfaceControllerTypeCollection) { // exclude front-end api controllers PluginControllerMetadata meta = PluginController.GetMetadata(controller); endpoints.MapUmbracoSurfaceRoute( meta.ControllerType, _umbracoPathSegment, meta.AreaName); } }
/// <summary> /// Returns the result of a child action of a SurfaceController /// </summary> public static IHtmlContent ActionLink(this IHtmlHelper htmlHelper, string actionName, Type surfaceType) { if (actionName == null) { throw new ArgumentNullException(nameof(actionName)); } if (string.IsNullOrWhiteSpace(actionName)) { throw new ArgumentException( "Value can't be empty or consist only of white-space characters.", nameof(actionName)); } if (surfaceType == null) { throw new ArgumentNullException(nameof(surfaceType)); } SurfaceControllerTypeCollection surfaceControllerTypeCollection = GetRequiredService <SurfaceControllerTypeCollection>(htmlHelper); Type?surfaceController = surfaceControllerTypeCollection.SingleOrDefault(x => x == surfaceType); if (surfaceController == null) { throw new InvalidOperationException("Could not find the surface controller of type " + surfaceType.FullName); } var routeVals = new RouteValueDictionary(new { area = string.Empty }); PluginControllerMetadata metaData = PluginController.GetMetadata(surfaceController); if (!metaData.AreaName.IsNullOrWhiteSpace()) { // set the area to the plugin area if (routeVals.ContainsKey("area")) { routeVals["area"] = metaData.AreaName; } else { routeVals.Add("area", metaData.AreaName); } } return(htmlHelper.ActionLink(actionName, metaData.ControllerName, routeVals)); }
/// <summary> /// Return the Url for a Web Api service /// </summary> /// <param name="url"></param> /// <param name="umbracoApiControllerTypeCollection"></param> /// <param name="actionName"></param> /// <param name="apiControllerType"></param> /// <param name="id"></param> /// <returns></returns> public static string?GetUmbracoApiService( this IUrlHelper url, UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection, string actionName, Type apiControllerType, object?id = null) { if (actionName == null) { throw new ArgumentNullException(nameof(actionName)); } if (string.IsNullOrWhiteSpace(actionName)) { throw new ArgumentException( "Value can't be empty or consist only of white-space characters.", nameof(actionName)); } if (apiControllerType == null) { throw new ArgumentNullException(nameof(apiControllerType)); } var area = string.Empty; Type?apiController = umbracoApiControllerTypeCollection.SingleOrDefault(x => x == apiControllerType); if (apiController == null) { throw new InvalidOperationException("Could not find the umbraco api controller of type " + apiControllerType.FullName); } PluginControllerMetadata metaData = PluginController.GetMetadata(apiController); if (metaData.AreaName.IsNullOrWhiteSpace() == false) { // set the area to the plugin area area = metaData.AreaName; } return(url.GetUmbracoApiService(actionName, ControllerExtensions.GetControllerName(apiControllerType), area !, id)); }
/// <summary> /// Auto-routes all front-end api controllers /// </summary> private void AutoRouteFrontEndApiControllers(IEndpointRouteBuilder endpoints) { foreach (Type controller in _apiControllers) { PluginControllerMetadata meta = PluginController.GetMetadata(controller); // exclude back-end api controllers if (meta.IsBackOffice) { continue; } endpoints.MapUmbracoApiRoute( meta.ControllerType, _umbracoPathSegment, meta.AreaName, meta.IsBackOffice, defaultAction: string.Empty); // no default action (this is what we had before) } }