Beispiel #1
0
        /// <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 = "";

            var apiController = umbracoApiControllerTypeCollection.SingleOrDefault(x => x == apiControllerType);

            if (apiController == null)
            {
                throw new InvalidOperationException("Could not find the umbraco api controller of type " + apiControllerType.FullName);
            }
            var 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));
        }
Beispiel #2
0
        /// <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));
        }
Beispiel #3
0
        /// <summary>
        /// Return the back office url if the back office is installed
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string?GetBackOfficeUrl(this IUrlHelper url)
        {
            var backOfficeControllerType = Type.GetType("Umbraco.Web.BackOffice.Controllers");

            if (backOfficeControllerType == null)
            {
                return("/");                                  // this would indicate that the installer is installed without the back office
            }
            return(url.Action("Default", ControllerExtensions.GetControllerName(backOfficeControllerType), new { area = Cms.Core.Constants.Web.Mvc.BackOfficeApiArea }));
        }
        /// <summary>
        /// Used to map Umbraco controllers consistently
        /// </summary>
        public static void MapUmbracoRoute(
            this IEndpointRouteBuilder endpoints,
            Type controllerType,
            string rootSegment,
            string?areaName,
            string?prefixPathSegment,
            string defaultAction = "Index",
            bool includeControllerNameInRoute = true,
            object?constraints = null)
        {
            var controllerName = ControllerExtensions.GetControllerName(controllerType);

            // build the route pattern
            var pattern = new StringBuilder(rootSegment);

            if (!prefixPathSegment.IsNullOrWhiteSpace())
            {
                pattern.Append('/').Append(prefixPathSegment);
            }

            if (includeControllerNameInRoute)
            {
                pattern.Append('/').Append(controllerName);
            }

            pattern.Append("/{action}/{id?}");

            var defaults = defaultAction.IsNullOrWhiteSpace()
                ? (object)new { controller = controllerName }
                : new { controller = controllerName, action = defaultAction };

            if (areaName.IsNullOrWhiteSpace())
            {
                endpoints.MapControllerRoute(

                    // named consistently
                    $"umbraco-{areaName}-{controllerName}".ToLowerInvariant(),
                    pattern.ToString().ToLowerInvariant(),
                    defaults,
                    constraints);
            }
            else
            {
                endpoints.MapAreaControllerRoute(

                    // named consistently
                    $"umbraco-{areaName}-{controllerName}".ToLowerInvariant(),
                    areaName !,
                    pattern.ToString().ToLowerInvariant(),
                    defaults,
                    constraints);
            }
        }
        /// <summary>
        /// Outputs the hidden html input field for Surface Controller route information
        /// </summary>
        /// <typeparam name="TSurface">The <see cref="SurfaceController"/> type</typeparam>
        /// <remarks>
        /// Typically not used directly because BeginUmbracoForm automatically outputs this value when routing
        /// for surface controllers. But this could be used in case a form tag is manually created.
        /// </remarks>
        public static IHtmlContent SurfaceControllerHiddenInput <TSurface>(
            this IHtmlHelper htmlHelper,
            string controllerAction,
            string area,
            object?additionalRouteVals = null)
            where TSurface : SurfaceController
        {
            var inputField = GetSurfaceControllerHiddenInput(
                GetRequiredService <IDataProtectionProvider>(htmlHelper),
                ControllerExtensions.GetControllerName <TSurface>(),
                controllerAction,
                area,
                additionalRouteVals);

            return(new HtmlString(inputField));
        }
Beispiel #6
0
        /// <summary>
        /// Return the back office url if the back office is installed
        /// </summary>
        public static string GetBackOfficeUrl(this LinkGenerator linkGenerator, IHostingEnvironment hostingEnvironment)
        {
            Type backOfficeControllerType;

            try
            {
                backOfficeControllerType = Assembly.Load("Umbraco.Web.BackOffice")?.GetType("Umbraco.Web.BackOffice.Controllers.BackOfficeController");
                if (backOfficeControllerType == null)
                {
                    return("/"); // this would indicate that the installer is installed without the back office
                }
            }
            catch
            {
                return(hostingEnvironment.ApplicationVirtualPath); // this would indicate that the installer is installed without the back office
            }

            return(linkGenerator.GetPathByAction("Default", ControllerExtensions.GetControllerName(backOfficeControllerType), values: new { area = Cms.Core.Constants.Web.Mvc.BackOfficeApiArea }));
        }
Beispiel #7
0
 /// <summary>
 /// Returns the URL for the installer api
 /// </summary>
 public static string GetInstallerApiUrl(this LinkGenerator linkGenerator)
 => linkGenerator.GetPathByAction(
     nameof(InstallApiController.GetSetup),
     ControllerExtensions.GetControllerName <InstallApiController>(),
     new { area = Cms.Core.Constants.Web.Mvc.InstallArea }).TrimEnd(nameof(InstallApiController.GetSetup));
Beispiel #8
0
 /// <summary>
 /// Returns the URL for the installer
 /// </summary>
 public static string GetInstallerUrl(this LinkGenerator linkGenerator)
 => linkGenerator.GetPathByAction(nameof(InstallController.Index), ControllerExtensions.GetControllerName <InstallController>(), new { area = Cms.Core.Constants.Web.Mvc.InstallArea });