public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            // If no document found anyway, then it will always be a match
            TreeNode FoundDoc = DocumentQueryHelper.GetNodeByAliasPath(EnvironmentHelper.GetUrl(httpContext.Request));

            return(FoundDoc != null && (FoundDoc.NodeAliasPath != "/" || !IgnoreRootPage));
        }
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            // Don't use dynamic routing for this {controller}/* route. 'KenticoFormWidget' is the OOB Kentico Forms widget in the default installation.
            string ControllerName = (values.ContainsKey("controller") ? ValidationHelper.GetString(values["controller"], "") : "");

            if (ControllerName.Equals("KenticoFormWidget", StringComparison.InvariantCultureIgnoreCase))
            {
                return(false);
            }

            // If no document found anyway, then it will always be a match
            TreeNode FoundDoc = DocumentQueryHelper.GetNodeByAliasPath(EnvironmentHelper.GetUrl(httpContext.Request));

            return(FoundDoc != null && (FoundDoc.NodeAliasPath != "/" || !IgnoreRootPage));
        }
Exemplo n.º 3
0
        public void ProcessRequest(HttpContext context)
        {
            IController        controller = null;
            IControllerFactory factory    = ControllerBuilder.Current.GetControllerFactory();

            string DefaultController = (RequestContext.RouteData.Values.ContainsKey("controller") ? RequestContext.RouteData.Values["controller"].ToString() : "");
            string DefaultAction     = (RequestContext.RouteData.Values.ContainsKey("action") ? RequestContext.RouteData.Values["action"].ToString() : "");
            string NewController     = "";
            string NewAction         = "Index";
            // Get the classname based on the URL
            TreeNode FoundNode = DocumentQueryHelper.GetNodeByAliasPath(context.Request.Url.AbsolutePath);
            string   ClassName = FoundNode.ClassName;

            switch (ClassName.ToLower())
            {
            case "":
                break;

            // can add your own cases to do more advanced logic if you wish
            default:
                // Default will look towards the classname (period replaced with _) for the Controller
                NewController = ClassName.Replace(".", "_");
                break;
            }

            // Controller not found, use defaults
            if (string.IsNullOrWhiteSpace(NewController))
            {
                NewController = DefaultController;
                NewAction     = DefaultAction;
            }

            // Setup routing with new values
            this.RequestContext.RouteData.Values["Controller"] = NewController;

            // If there is an action (2nd value), change it to the CheckNotFound, and remove ID
            if (this.RequestContext.RouteData.Values.ContainsKey("Action"))
            {
                this.RequestContext.RouteData.Values["Action"] = NewAction;
            }
            else
            {
                this.RequestContext.RouteData.Values.Add("Action", NewAction);
            }
            if (RequestContext.RouteData.Values.ContainsKey("Id"))
            {
                RequestContext.RouteData.Values.Remove("Id");
            }
            try
            {
                controller = factory.CreateController(RequestContext, NewController);
                controller.Execute(RequestContext);
            }
            catch (HttpException ex)
            {
                // Even that failed, log and use normal HttpErrors controller
                EventLogProvider.LogException("KMVCDynamicHttpHandler", "ClassControllerNotConfigured", ex, additionalMessage: "Page found, but could not find a Controller for " + NewController + ", create one with an index view to auto handle or modify the KMVCDynamicHttpHandler");
                RequestContext.RouteData.Values["Controller"] = DefaultController;
                RequestContext.RouteData.Values["Action"]     = DefaultAction;
                controller = factory.CreateController(RequestContext, DefaultController);
                controller.Execute(RequestContext);
            }
            factory.ReleaseController(controller);
        }
        public void ProcessRequest(HttpContext context)
        {
            IController        controller = null;
            IControllerFactory factory    = ControllerBuilder.Current.GetControllerFactory();

            string DefaultController = (RequestContext.RouteData.Values.ContainsKey("controller") ? RequestContext.RouteData.Values["controller"].ToString() : "");
            string DefaultAction     = (RequestContext.RouteData.Values.ContainsKey("action") ? RequestContext.RouteData.Values["action"].ToString() : "");
            string NewController     = "";
            string NewAction         = "Index";
            // Get the classname based on the URL
            TreeNode FoundNode = DocumentQueryHelper.GetNodeByAliasPath(EnvironmentHelper.GetUrl(context.Request));
            string   ClassName = FoundNode.ClassName;


            switch (ClassName.ToLower())
            {
            default:
                //
                if (PageHasTemplate(FoundNode))
                {
                    // Uses Page Templates, send to basic Page Template handler
                    NewController = "DynamicPageTemplate";
                    NewAction     = "Index";
                }
                else
                {
                    // Try finding a class that matches the class name
                    NewController = ClassName.Replace(".", "_");
                }
                break;

            // can add your own cases to do more advanced logic if you wish
            case "":
                break;
            }

            // Controller not found, use defaults
            if (string.IsNullOrWhiteSpace(NewController))
            {
                NewController = DefaultController;
                NewAction     = DefaultAction;
            }

            // Setup routing with new values
            this.RequestContext.RouteData.Values["Controller"] = NewController;

            // If there is an action (2nd value), change it to the CheckNotFound, and remove ID
            if (this.RequestContext.RouteData.Values.ContainsKey("Action"))
            {
                this.RequestContext.RouteData.Values["Action"] = NewAction;
            }
            else
            {
                this.RequestContext.RouteData.Values.Add("Action", NewAction);
            }
            if (RequestContext.RouteData.Values.ContainsKey("Id"))
            {
                RequestContext.RouteData.Values.Remove("Id");
            }
            try
            {
                controller = factory.CreateController(RequestContext, NewController);
                controller.Execute(RequestContext);
            }
            catch (HttpException ex)
            {
                // Catch Controller Not implemented errors and log and go to Not Foud
                if (ex.Message.ToLower().Contains("does not implement icontroller."))
                {
                    EventLogProvider.LogException("KMVCDynamicHttpHandler", "ClassControllerNotConfigured", ex, additionalMessage: "Page found, but could not find Page Templates, nor a Controller for " + NewController + ", either create Page Templates for this class or create a controller with an index view to auto handle or modify the KMVCDynamicHttpHandler");
                    RequestContext.RouteData.Values["Controller"] = "DynamicPageTemplate";
                    RequestContext.RouteData.Values["Action"]     = "NotFound";
                    controller = factory.CreateController(RequestContext, "DynamicPageTemplate");
                    controller.Execute(RequestContext);
                }
                else
                {
                    // This will show for any http generated exception, like view errors
                    throw new HttpException(ex.Message, ex);
                }
            }
            catch (InvalidOperationException ex)
            {
                if (ex.Message.ToLower().Contains("page template with identifier"))
                {
                    // This often occurs when there is a page template assigned that is not defined
                    EventLogProvider.LogException("KMVCDynamicHttpHandler", "ClassControllerNotConfigured", ex, additionalMessage: "Page found, but contains a template that is not registered with this application.");
                    RequestContext.RouteData.Values["Controller"] = "DynamicPageTemplate";
                    RequestContext.RouteData.Values["Action"]     = "UnregisteredTemplate";
                    controller = factory.CreateController(RequestContext, "DynamicPageTemplate");
                    controller.Execute(RequestContext);
                }
                else
                {
                    throw new InvalidOperationException(ex.Message, ex);
                }
            }
            factory.ReleaseController(controller);
        }