Exemple #1
0
        public T GetModel <T>(HttpContextBase httpContextBase) where T : class, new()
        {
            var currentRouteData = httpContextBase.Request.RequestContext.RouteData;

            Ensure.ArgumentIsNotNull(currentRouteData, "currentRouteData");

            var routeDataValues = currentRouteData.Values.ToList();

            var cmsRouteDataValues = GetCmsRouteDataValues(routeDataValues);

            var controllerName = cmsRouteDataValues[0].Value.ToString();

            var sectionNode = _sectionNodeService.GetByUrlName(controllerName);

            if (sectionNode == null)
            {
                throw new ArgumentException(string.Format(Messages.SectionNodeNotFoundForUrlName, controllerName));
            }

            PageNode pageNode = null;

            for (var i = 0; i < cmsRouteDataValues.Count; i++)
            {
                if (i == 0)
                {
                    continue;          //skip the controller route data value
                }
                pageNode = i == 1
                            ? NodeHelper.GetActionPageNode(sectionNode.PageNodes, cmsRouteDataValues[i].Value.ToString())
                            : NodeHelper.GetActionPageNode(pageNode.PageNodes, cmsRouteDataValues[i].Value.ToString());

                if (pageNode == null)
                {
                    throw new ArgumentException(string.Format("The page with the url name : {0} was not found.", cmsRouteDataValues[i].Value));
                }
            }

            var modelName = pageNode.PageTemplate.ModelName;

            var modelType     = Assembly.GetCallingAssembly().GetModels().FirstOrDefault(x => x.Name == modelName);
            var modelInstance = (T)Activator.CreateInstance(modelType);

            var props           = modelType.GetMembers();
            var modelProperties = props.Where(x => x.GetCustomAttributes(typeof(CmsModelPropertyAttribute), false).Length > 0).OfType <PropertyInfo>().ToList();

            foreach (var property in pageNode.Properties)
            {
                var modelProperty = modelProperties.FirstOrDefault(x => x.Name == property.PropertyTemplate.PropertyName);
                if (modelProperty != null)
                {
                    modelProperty.SetValue(modelInstance, property.Text, BindingFlags.Public, null, null, null);
                }
            }

            return(modelInstance);
        }
Exemple #2
0
        public override bool InvokeAction(ControllerContext controllerContext, string actionName)
        {
            if (controllerContext == null)
            {
                return(false);
            }

            var currentRouteData = controllerContext.RouteData;

            if (currentRouteData == null)
            {
                return(false);
            }

            var routeDataValues = currentRouteData.Values.ToList();

            var controllerName = currentRouteData.GetRequiredString("controller");

            var sectionNode = _sectionNodeService.GetByUrlName(controllerName);

            if (sectionNode == null)
            {
                throw new ArgumentException(string.Format(Messages.SectionNodeNotFoundForUrlName, controllerName));
            }

            PageNode pageNode = null;

            currentRouteData.Values[CmsRoutingConstants.RouteDataNameForSection] = sectionNode.UrlName;

            for (var i = 0; i < routeDataValues.Count; i++)
            {
                if (i == 0)
                {
                    continue;          //skip the controller route data value
                }
                pageNode = i == 1
                            ? NodeHelper.GetActionPageNode(sectionNode.PageNodes, routeDataValues[i].Value.ToString())
                            : NodeHelper.GetActionPageNode(pageNode.PageNodes, routeDataValues[i].Value.ToString());

                currentRouteData.Values[string.Format(CmsRoutingConstants.RouteDataNameForAction + "-{0}", i)] = pageNode.UrlName;
            }

            //set the original controller and action values back to normal so MVC can find the appropriate view
            //also set new values to store the cms version of the controller and action
            currentRouteData.Values["controller"] = sectionNode.SectionTemplate.ControllerName.Replace("Controller", "");
            currentRouteData.Values["action"]     = pageNode.PageTemplate.ActionName;

            return(base.InvokeAction(controllerContext, pageNode.PageTemplate.ActionName));
        }