Example #1
0
        public virtual ActionResult Index(IUmbracoRenderModel model)
        {
            if (model.CurrentNode == null)
            {
                return(new HttpNotFoundResult());
            }

            using (var uow = RoutableRequestContext.Application.Hive.OpenReader <IFileStore>(new Uri("storage://templates")))
            {
                var templateFile = model.CurrentNode.CurrentTemplate != null
                                   ? uow.Repositories.Get <File>(model.CurrentNode.CurrentTemplate.Id)
                                   : null;

                if (templateFile != null)
                {
                    //try to find the view based on all engines registered.
                    var view = MSMvc.ViewEngines.Engines.FindView(ControllerContext,
                                                                  Path.GetFileNameWithoutExtension(templateFile.RootedPath), "");

                    if (view.View != null)
                    {
                        return(View(view.View, model.CurrentNode));
                    }
                }

                //return the compiled default view!
                //TODO: Remove magic strings
                return(View(
                           EmbeddedViewPath.Create("Umbraco.Cms.Web.EmbeddedViews.Views.TemplateNotFound.cshtml,Umbraco.Cms.Web"),
                           model.CurrentNode));
            }
        }
 public ActionResult HomePage(IUmbracoRenderModel model)
 {
     return(View());
 }
        /// <summary>
        /// Returns a RouteDefinition object based on the current renderModel
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="renderModel"></param>
        /// <returns></returns>
        protected virtual RouteDefinition GetUmbracoRouteDefinition(RequestContext requestContext, IUmbracoRenderModel renderModel)
        {
            var def = new RouteDefinition
            {
                ControllerName = UmbracoController.GetControllerName <UmbracoController>(),
                Controller     = new UmbracoController(),
                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);

                    //TODO: We need to add internal caching to the below, once we know that the controller can be
                    // created, we shouldn't need to try to create it again,
                    // also, once we determine that the Action to route to, that should be cached too.

                    //check if there's a custom controller assigned
                    var controller = _controllerFactory.CreateController(requestContext, def.ControllerName);

                    //check if that controller exists
                    if (controller != null)
                    {
                        //ensure the controller is of type 'UmbracoController'
                        if (!(controller is UmbracoController))
                        {
                            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}'.", renderModel.CurrentNode.ContentType.Alias, controller.GetType().FullName, typeof(UmbracoController).FullName);
                            //exit as we cannnot route to the custom controller
                            return(def);
                        }

                        def.ControllerName = renderModel.CurrentNode.ContentType.Alias;
                        def.Controller     = (ControllerBase)controller;
                        def.ControllerName = UmbracoController.GetControllerName(controller.GetType());

                        // 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 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 = template.Name.Split('.')[0].ToUmbracoAlias(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);
        }
        /// <summary>
        /// this will determine the controller and set the values in the route data
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="renderModel"></param>
        protected internal IHttpHandler GetHandlerForRoute(RequestContext requestContext, IUmbracoRenderModel renderModel)
        {
            var routeDef = GetUmbracoRouteDefinition(requestContext, renderModel);

            //Need to check for a special case if there is form data being posted back to an Umbraco URL
            var postedInfo = GetPostedFormInfo(requestContext);

            if (postedInfo != null)
            {
                return(HandlePostedValues(requestContext, postedInfo, renderModel, routeDef));
            }

            //no post values, just route to the controller/action requried (local)

            requestContext.RouteData.Values["controller"] = routeDef.ControllerName;
            if (!routeDef.ActionName.IsNullOrWhiteSpace())
            {
                requestContext.RouteData.Values["action"] = routeDef.ActionName;
            }
            return(new MvcHandler(requestContext));
        }
        /// <summary>
        /// Handles a posted form to an Umbraco 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, IUmbracoRenderModel 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["umbraco-item-url"] = requestContext.HttpContext.Request.Url.AbsolutePath;
            //store the original route definition
            requestContext.RouteData.DataTokens["umbraco-route-def"] = routeDefinition;

            return(handler);
        }
 public ActionResult HomePage(IUmbracoRenderModel model)
 {
     return View();
 }
        /// <summary>
        /// this will determine the controller and set the values in the route data
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="renderModel"></param>
        protected internal IHttpHandler GetHandlerForRoute(RequestContext requestContext, IUmbracoRenderModel renderModel)
        {
            var routeDef = GetUmbracoRouteDefinition(requestContext, renderModel);

            //Need to check for a special case if there is form data being posted back to an Umbraco URL
            var postedInfo = GetPostedFormInfo(requestContext);
            if (postedInfo != null)
            {
                return HandlePostedValues(requestContext, postedInfo, renderModel, routeDef);    
            }

            //no post values, just route to the controller/action requried (local)

            requestContext.RouteData.Values["controller"] = routeDef.ControllerName;
            if (!routeDef.ActionName.IsNullOrWhiteSpace())
            {
                requestContext.RouteData.Values["action"] = routeDef.ActionName;
            }
            return new MvcHandler(requestContext);
        }
        /// <summary>
        /// Returns a RouteDefinition object based on the current renderModel
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="renderModel"></param>
        /// <returns></returns>
        protected virtual RouteDefinition GetUmbracoRouteDefinition(RequestContext requestContext, IUmbracoRenderModel renderModel)
        {
            //creates the default route definition which maps to the 'UmbracoController' controller
            var def = new RouteDefinition
                {
                    ControllerName = UmbracoController.GetControllerName<UmbracoController>(), 
                    Controller = new UmbracoController(),
                    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 'UmbracoController'
                        if (controller is UmbracoController)
                        {
                            //set the controller and name to the custom one
                            def.Controller = (ControllerBase)controller;
                            def.ControllerName = UmbracoController.GetControllerName(controller.GetType());    
                        }
                        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}'.", renderModel.CurrentNode.ContentType.Alias, controller.GetType().FullName, typeof(UmbracoController).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 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 = template.Name.Split('.')[0].ToUmbracoAlias(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;
        }
        /// <summary>
        /// Handles a posted form to an Umbraco 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, IUmbracoRenderModel 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["umbraco-item-url"] = requestContext.HttpContext.Request.Url.AbsolutePath;
            //store the original route definition
            requestContext.RouteData.DataTokens["umbraco-route-def"] = routeDefinition;

            return handler;
        }