Beispiel #1
0
        /// <summary>
        /// Creates the specified controller by using the specified request context.
        /// </summary>
        /// <returns>
        /// The controller.
        /// </returns>
        /// <param name="requestContext">The request context.</param><param name="controllerName">The name of the controller.</param>
        public virtual IController CreateController(RequestContext requestContext, string controllerName)
        {
            Type controllerType = GetControllerType(requestContext, controllerName) ??
                                  _innerFactory.GetControllerType(requestContext, ControllerExtensions.GetControllerName(typeof(RenderMvcController)));

            return(_innerFactory.GetControllerInstance(requestContext, controllerType));
        }
Beispiel #2
0
        /// <summary>
        /// Returns the metadata for a PluginController
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static PluginControllerMetadata GetMetadata(Type type)
        {
            if (!TypeHelper.IsTypeAssignableFrom <PluginController>(type))
            {
                throw new InvalidOperationException("Cannot get metadata from a type that is not a PluginController");
            }

            PluginControllerMetadata meta;

            if (MetadataStorage.TryGetValue(type, out meta))
            {
                return(meta);
            }

            var attribute = type.GetCustomAttribute <PluginControllerAttribute>(false);

            meta = new PluginControllerMetadata()
            {
                AreaName            = attribute == null ? null : attribute.AreaName,
                ControllerName      = ControllerExtensions.GetControllerName(type),
                ControllerNamespace = type.Namespace,
                ControllerType      = type
            };

            MetadataStorage.TryAdd(type, meta);

            return(meta);
        }
Beispiel #3
0
        /// <summary>
        /// Returns a RouteDefinition object based on the current renderModel
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="publishedContentRequest"></param>
        /// <returns></returns>
        internal virtual RouteDefinition GetUmbracoRouteDefinition(RequestContext requestContext, PublishedContentRequest publishedContentRequest)
        {
            var defaultControllerName = ControllerExtensions.GetControllerName <RenderMvcController>();
            //creates the default route definition which maps to the 'UmbracoController' controller
            var def = new RouteDefinition
            {
                ControllerName          = defaultControllerName,
                Controller              = new RenderMvcController(),
                PublishedContentRequest = publishedContentRequest,
                ActionName              = ((Route)requestContext.RouteData.Route).Defaults["action"].ToString(),
                HasHijackedRoute        = false
            };

            //check if there's a custom controller assigned, base on the document type alias.
            var controller = _controllerFactory.CreateController(requestContext, publishedContentRequest.PublishedContent.DocumentTypeAlias);


            //check if that controller exists
            if (controller != null)
            {
                //ensure the controller is of type 'RenderMvcController'
                if (controller is RenderMvcController)
                {
                    //set the controller and name to the custom one
                    def.Controller     = (ControllerBase)controller;
                    def.ControllerName = ControllerExtensions.GetControllerName(controller.GetType());
                    if (def.ControllerName != defaultControllerName)
                    {
                        def.HasHijackedRoute = true;
                    }
                }
                else
                {
                    LogHelper.Warn <RenderRouteHandler>(
                        "The current Document Type {0} matches a locally declared controller of type {1}. Custom Controllers for Umbraco routing must inherit from '{2}'.",
                        () => publishedContentRequest.PublishedContent.DocumentTypeAlias,
                        () => controller.GetType().FullName,
                        () => typeof(RenderMvcController).FullName);
                    //exit as we cannnot route to the custom controller, just route to the standard one.
                    return(def);
                }

                //check that a template is defined), if it doesn't and there is a hijacked route it will just route
                // to the index Action
                if (publishedContentRequest.HasTemplate)
                {
                    //the template Alias should always be already saved with a safe name.
                    //if there are hyphens in the name and there is a hijacked route, then the Action will need to be attributed
                    // with the action name attribute.
                    var templateName = global::umbraco.cms.helpers.Casing.SafeAlias(publishedContentRequest.Template.Alias.Split('.')[0]);
                    def.ActionName = templateName;
                }
            }

            //store the route definition
            requestContext.RouteData.DataTokens["umbraco-route-def"] = def;

            return(def);
        }
Beispiel #4
0
        /// <summary>
        /// Creates the specified controller by using the specified request context.
        /// </summary>
        /// <returns>
        /// The controller.
        /// </returns>
        /// <param name="requestContext">The request context.</param><param name="controllerName">The name of the controller.</param>
        public virtual IController CreateController(RequestContext requestContext, string controllerName)
        {
            var controllerType = GetControllerType(requestContext, controllerName) ??
                                 _innerFactory.GetControllerType(
                requestContext,
                ControllerExtensions.GetControllerName(Current.DefaultRenderMvcControllerType));

            return(_innerFactory.GetControllerInstance(requestContext, controllerType));
        }
Beispiel #5
0
        /// <summary>
        /// Returns a RouteDefinition object based on the current renderModel
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="publishedContentRequest"></param>
        /// <returns></returns>
        internal virtual RouteDefinition GetUmbracoRouteDefinition(RequestContext requestContext, PublishedContentRequest publishedContentRequest)
        {
            var defaultControllerName = ControllerExtensions.GetControllerName <RenderMvcController>();
            //creates the default route definition which maps to the 'UmbracoController' controller
            var def = new RouteDefinition
            {
                ControllerName          = defaultControllerName,
                Controller              = new RenderMvcController(),
                PublishedContentRequest = publishedContentRequest,
                ActionName              = ((Route)requestContext.RouteData.Route).Defaults["action"].ToString(),
                HasHijackedRoute        = false
            };

            //check if there's a custom controller assigned, base on the document type alias.
            var controller = _controllerFactory.CreateController(requestContext, publishedContentRequest.PublishedContent.DocumentTypeAlias);


            //check if that controller exists
            if (controller != null)
            {
                //ensure the controller is of type 'RenderMvcController'
                if (controller is RenderMvcController)
                {
                    //set the controller and name to the custom one
                    def.Controller     = (ControllerBase)controller;
                    def.ControllerName = ControllerExtensions.GetControllerName(controller.GetType());
                    if (def.ControllerName != defaultControllerName)
                    {
                        def.HasHijackedRoute = true;
                    }
                }
                else
                {
                    LogHelper.Warn <RenderRouteHandler>("The current Document Type {0} matches a locally declared controller of type {1}. Custom Controllers for Umbraco routing must inherit from '{2}'.", publishedContentRequest.PublishedContent.DocumentTypeAlias, controller.GetType().FullName, typeof(RenderMvcController).FullName);
                    //exit as we cannnot route to the custom controller, just route to the standard one.
                    return(def);
                }

                //check that a template is defined), if it doesn't and there is a hijacked route it will just route
                // to the index Action
                if (publishedContentRequest.HasTemplate)
                {
                    //check if the custom controller has an action with the same name as the template name (we convert ToUmbracoAlias since the template name might have invalid chars).
                    //NOTE: This also means that all custom actions MUST be PascalCase.. but that should be standard.
                    var templateName = publishedContentRequest.Template.Alias.Split('.')[0].ToUmbracoAlias(StringAliasCaseType.PascalCase);
                    def.ActionName = templateName;
                }
            }


            return(def);
        }
        /// <summary>
        /// Gets metadata for a controller type.
        /// </summary>
        /// <param name="controllerType">The controller type.</param>
        /// <returns>Metadata for the controller type.</returns>
        internal static PluginControllerMetadata GetMetadata(Type controllerType)
        {
            return(MetadataStorage.GetOrAdd(controllerType, type =>
            {
                // plugin controller? back-office controller?
                var pluginAttribute = controllerType.GetCustomAttribute <PluginControllerAttribute>(false);
                var backOfficeAttribute = controllerType.GetCustomAttribute <IsBackOfficeAttribute>(true);

                return new PluginControllerMetadata
                {
                    AreaName = pluginAttribute?.AreaName,
                    ControllerName = ControllerExtensions.GetControllerName(controllerType),
                    ControllerNamespace = controllerType.Namespace,
                    ControllerType = controllerType,
                    IsBackOffice = backOfficeAttribute != null
                };
            }));
        }
Beispiel #7
0
        /// <summary>
        /// Returns the metadata for a PluginController
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static PluginControllerMetadata GetMetadata(Type type)
        {
            return(MetadataStorage.GetOrAdd(type, type1 =>
            {
                var attribute = type.GetCustomAttribute <PluginControllerAttribute>(false);

                var meta = new PluginControllerMetadata()
                {
                    AreaName = attribute == null ? null : attribute.AreaName,
                    ControllerName = ControllerExtensions.GetControllerName(type),
                    ControllerNamespace = type.Namespace,
                    ControllerType = type
                };

                MetadataStorage.TryAdd(type, meta);

                return meta;
            }));
        }
        /// <summary>
        /// Returns the metadata for a PluginController
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static PluginControllerMetadata GetMetadata(Type type)
        {
            return(MetadataStorage.GetOrAdd(type, type1 =>
            {
                var pluginAttribute = type.GetCustomAttribute <PluginControllerAttribute>(false);
                //check if any inherited class of this type contains the IsBackOffice attribute
                var backOfficeAttribute = type.GetCustomAttribute <IsBackOfficeAttribute>(true);

                var meta = new PluginControllerMetadata()
                {
                    AreaName = pluginAttribute == null ? null : pluginAttribute.AreaName,
                    ControllerName = ControllerExtensions.GetControllerName(type),
                    ControllerNamespace = type.Namespace,
                    ControllerType = type,
                    IsBackOffice = backOfficeAttribute != null
                };

                MetadataStorage.TryAdd(type, meta);

                return meta;
            }));
        }
Beispiel #9
0
        /// <summary>
        /// Returns a RouteDefinition object based on the current content request
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        internal virtual RouteDefinition GetUmbracoRouteDefinition(RequestContext requestContext, PublishedRequest request)
        {
            if (requestContext == null)
            {
                throw new ArgumentNullException(nameof(requestContext));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var defaultControllerType = Current.DefaultRenderMvcControllerType;
            var defaultControllerName = ControllerExtensions.GetControllerName(defaultControllerType);
            //creates the default route definition which maps to the 'UmbracoController' controller
            var def = new RouteDefinition
            {
                ControllerName   = defaultControllerName,
                ControllerType   = defaultControllerType,
                PublishedRequest = request,
                ActionName       = ((Route)requestContext.RouteData.Route).Defaults["action"].ToString(),
                HasHijackedRoute = false
            };

            //check that a template is defined), if it doesn't and there is a hijacked route it will just route
            // to the index Action
            if (request.HasTemplate)
            {
                //the template Alias should always be already saved with a safe name.
                //if there are hyphens in the name and there is a hijacked route, then the Action will need to be attributed
                // with the action name attribute.
                var templateName = request.TemplateAlias.Split('.')[0].ToSafeAlias();
                def.ActionName = templateName;
            }

            //check if there's a custom controller assigned, base on the document type alias.
            var controllerType = _controllerFactory.GetControllerTypeInternal(requestContext, request.PublishedContent.ContentType.Alias);

            //check if that controller exists
            if (controllerType != null)
            {
                //ensure the controller is of type IRenderMvcController and ControllerBase
                if (TypeHelper.IsTypeAssignableFrom <IRenderController>(controllerType) &&
                    TypeHelper.IsTypeAssignableFrom <ControllerBase>(controllerType))
                {
                    //set the controller and name to the custom one
                    def.ControllerType = controllerType;
                    def.ControllerName = ControllerExtensions.GetControllerName(controllerType);
                    if (def.ControllerName != defaultControllerName)
                    {
                        def.HasHijackedRoute = true;
                    }
                }
                else
                {
                    Current.Logger.Warn <RenderRouteHandler>("The current Document Type {ContentTypeAlias} matches a locally declared controller of type {ControllerName}. Custom Controllers for Umbraco routing must implement '{UmbracoRenderController}' and inherit from '{UmbracoControllerBase}'.",
                                                             request.PublishedContent.ContentType.Alias,
                                                             controllerType.FullName,
                                                             typeof(IRenderController).FullName,
                                                             typeof(ControllerBase).FullName);

                    //we cannot route to this custom controller since it is not of the correct type so we'll continue with the defaults
                    // that have already been set above.
                }
            }

            //store the route definition
            requestContext.RouteData.DataTokens[Core.Constants.Web.UmbracoRouteDefinitionDataToken] = def;

            return(def);
        }