Represents the data required to route to a specific controller/action during an Rebel request
コード例 #1
0
        /// <summary>
        /// Returns a RouteDefinition object based on the current renderModel
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="renderModel"></param>
        /// <returns></returns>
        protected virtual RouteDefinition GetRebelRouteDefinition(RequestContext requestContext, IRebelRenderModel renderModel)
        {
            //creates the default route definition which maps to the 'RebelController' controller
            var def = new RouteDefinition
            {
                ControllerName = RebelController.GetControllerName <RebelController>(),
                Controller     = new RebelController(),
                RenderModel    = renderModel,
                ActionName     = ((Route)requestContext.RouteData.Route).Defaults["action"].ToString()
            };

            //check that a template is defined)
            if (renderModel.CurrentNode != null && renderModel.CurrentNode.CurrentTemplate != null)
            {
                using (var uow = _applicationContext.Hive.OpenReader <IFileStore>(new Uri("storage://templates")))
                {
                    var template = uow.Repositories.Get <File>(renderModel.CurrentNode.CurrentTemplate.Id);

                    //check if there's a custom controller assigned, base on the document type alias.
                    var controller = _controllerFactory.CreateController(requestContext, renderModel.CurrentNode.ContentType.Alias);

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

                        // Template might be null if none is assigned to the node
                        if (template != null)
                        {
                            //check if the custom controller has an action with the same name as the template name (we convert ToRebelAlias 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 = template.Name.Split('.')[0].ToRebelAlias(StringAliasCaseType.PascalCase);
                            var action       = controller.GetType().GetMethod(templateName);
                            if (action != null)
                            {
                                def.ActionName = templateName;
                            }
                            //otherwise it will continue to route to Index since thats teh default route
                        }
                    }
                }
            }

            return(def);
        }
コード例 #2
0
        /// <summary>
        /// Returns a RouteDefinition object based on the current renderModel
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="renderModel"></param>
        /// <returns></returns>
        protected virtual RouteDefinition GetRebelRouteDefinition(RequestContext requestContext, IRebelRenderModel renderModel)
        {
            //creates the default route definition which maps to the 'RebelController' controller
            var def = new RouteDefinition
                {
                    ControllerName = RebelController.GetControllerName<RebelController>(), 
                    Controller = new RebelController(),
                    RenderModel = renderModel,
                    ActionName = ((Route)requestContext.RouteData.Route).Defaults["action"].ToString()
                };

            //check that a template is defined)
            if (renderModel.CurrentNode != null && renderModel.CurrentNode.CurrentTemplate != null)
            {
                using (var uow = _applicationContext.Hive.OpenReader<IFileStore>(new Uri("storage://templates")))
                {
                    var template = uow.Repositories.Get<File>(renderModel.CurrentNode.CurrentTemplate.Id);
                    
                    //check if there's a custom controller assigned, base on the document type alias.
                    var controller = _controllerFactory.CreateController(requestContext, renderModel.CurrentNode.ContentType.Alias);

                    //check if that controller exists
                    if (controller != null)
                    {                        

                        //ensure the controller is of type 'RebelController'
                        if (controller is RebelController)
                        {
                            //set the controller and name to the custom one
                            def.Controller = (ControllerBase)controller;
                            def.ControllerName = RebelController.GetControllerName(controller.GetType());    
                        }
                        else
                        {
                            LogHelper.Warn<RenderRouteHandler>("The current Document Type {0} matches a locally declared controller of type {1}. Custom Controllers for Rebel routing must inherit from '{2}'.", renderModel.CurrentNode.ContentType.Alias, controller.GetType().FullName, typeof(RebelController).FullName);
                            //exit as we cannnot route to the custom controller, just route to the standard one.
                            return def;
                        }
                        
                        // Template might be null if none is assigned to the node
                        if (template != null)
                        {
                            //check if the custom controller has an action with the same name as the template name (we convert ToRebelAlias 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 = template.Name.Split('.')[0].ToRebelAlias(StringAliasCaseType.PascalCase);
                            var action = controller.GetType().GetMethod(templateName);
                            if (action != null)
                            {
                                def.ActionName = templateName;
                            }
                            //otherwise it will continue to route to Index since thats teh default route
                        }
                        
                    }
                }
            }

            return def;
        }
コード例 #3
0
        /// <summary>
        /// Handles a posted form to an Rebel Url and ensures the correct controller is routed to and that
        /// the right DataTokens are set.
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="postedInfo"></param>
        /// <param name="renderModel"></param>
        /// <param name="routeDefinition">The original route definition that would normally be used to route if it were not a POST</param>
        protected virtual IHttpHandler HandlePostedValues(RequestContext requestContext, PostedDataProxyInfo postedInfo, IRebelRenderModel renderModel, RouteDefinition routeDefinition)
        {
         
            //set the standard route values/tokens
            requestContext.RouteData.Values["controller"] = postedInfo.ControllerName;
            requestContext.RouteData.Values["action"] = postedInfo.ActionName;            
            requestContext.RouteData.DataTokens["area"] = postedInfo.Area;

            IHttpHandler handler = new MvcHandler(requestContext);

            //ensure the surface id is set if found, meaning it is a plugin, not locally declared
            if (postedInfo.SurfaceId != default(Guid))
            {
                requestContext.RouteData.Values["surfaceId"] = postedInfo.SurfaceId.ToString("N");
                //find the other data tokens for this route and merge... things like Namespace will be included here
                using (RouteTable.Routes.GetReadLock())
                {
                    var surfaceRoute = RouteTable.Routes.OfType<Route>()
                        .Where(x => x.Defaults != null && x.Defaults.ContainsKey("surfaceId") &&
                            x.Defaults["surfaceId"].ToString() == postedInfo.SurfaceId.ToString("N"))
                        .SingleOrDefault();
                    if (surfaceRoute == null)
                        throw new InvalidOperationException("Could not find a Surface controller route in the RouteTable for id " + postedInfo.SurfaceId);
                    //set the 'Namespaces' token so the controller factory knows where to look to construct it
                    if (surfaceRoute.DataTokens.ContainsKey("Namespaces"))
                    {
                        requestContext.RouteData.DataTokens["Namespaces"] = surfaceRoute.DataTokens["Namespaces"];    
                    }
                    handler = surfaceRoute.RouteHandler.GetHttpHandler(requestContext);
                }
                
            }

            //store the original URL this came in on
            requestContext.RouteData.DataTokens["rebel-item-url"] = requestContext.HttpContext.Request.Url.AbsolutePath;
            //store the original route definition
            requestContext.RouteData.DataTokens["rebel-route-def"] = routeDefinition;

            return handler;
        }
コード例 #4
0
        /// <summary>
        /// Handles a posted form to an Rebel Url and ensures the correct controller is routed to and that
        /// the right DataTokens are set.
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="postedInfo"></param>
        /// <param name="renderModel"></param>
        /// <param name="routeDefinition">The original route definition that would normally be used to route if it were not a POST</param>
        protected virtual IHttpHandler HandlePostedValues(RequestContext requestContext, PostedDataProxyInfo postedInfo, IRebelRenderModel renderModel, RouteDefinition routeDefinition)
        {
            //set the standard route values/tokens
            requestContext.RouteData.Values["controller"] = postedInfo.ControllerName;
            requestContext.RouteData.Values["action"]     = postedInfo.ActionName;
            requestContext.RouteData.DataTokens["area"]   = postedInfo.Area;

            IHttpHandler handler = new MvcHandler(requestContext);

            //ensure the surface id is set if found, meaning it is a plugin, not locally declared
            if (postedInfo.SurfaceId != default(Guid))
            {
                requestContext.RouteData.Values["surfaceId"] = postedInfo.SurfaceId.ToString("N");
                //find the other data tokens for this route and merge... things like Namespace will be included here
                using (RouteTable.Routes.GetReadLock())
                {
                    var surfaceRoute = RouteTable.Routes.OfType <Route>()
                                       .Where(x => x.Defaults != null && x.Defaults.ContainsKey("surfaceId") &&
                                              x.Defaults["surfaceId"].ToString() == postedInfo.SurfaceId.ToString("N"))
                                       .SingleOrDefault();
                    if (surfaceRoute == null)
                    {
                        throw new InvalidOperationException("Could not find a Surface controller route in the RouteTable for id " + postedInfo.SurfaceId);
                    }
                    //set the 'Namespaces' token so the controller factory knows where to look to construct it
                    if (surfaceRoute.DataTokens.ContainsKey("Namespaces"))
                    {
                        requestContext.RouteData.DataTokens["Namespaces"] = surfaceRoute.DataTokens["Namespaces"];
                    }
                    handler = surfaceRoute.RouteHandler.GetHttpHandler(requestContext);
                }
            }

            //store the original URL this came in on
            requestContext.RouteData.DataTokens["rebel-item-url"] = requestContext.HttpContext.Request.Url.AbsolutePath;
            //store the original route definition
            requestContext.RouteData.DataTokens["rebel-route-def"] = routeDefinition;

            return(handler);
        }