Ejemplo n.º 1
0
        /// <summary>
        /// Renders the current (Include) Page as a Region.
        /// </summary>
        /// <param name="htmlHelper">The HtmlHelper instance on which the extension method operates.</param>
        /// <returns>The rendered HTML.</returns>
        public static MvcHtmlString DxaRegion(this HtmlHelper htmlHelper)
        {
            using (new Tracer(htmlHelper))
            {
                PageModel pageModel = (PageModel)htmlHelper.ViewData.Model;

                // Create a new Region Model which reflects the Page Model
                string  regionName = pageModel.Title;
                MvcData mvcData    = new MvcData
                {
                    ViewName           = regionName,
                    AreaName           = SiteConfiguration.GetDefaultModuleName(),
                    ControllerName     = SiteConfiguration.GetRegionController(),
                    ControllerAreaName = SiteConfiguration.GetDefaultModuleName(),
                    ActionName         = SiteConfiguration.GetRegionAction()
                };

                RegionModel regionModel = new RegionModel(regionName)
                {
                    MvcData = mvcData
                };
                regionModel.Regions.UnionWith(pageModel.Regions);

                return(htmlHelper.DxaRegion(regionModel));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Renders a given Region Model
        /// </summary>
        /// <param name="htmlHelper">The HtmlHelper instance on which the extension method operates.</param>
        /// <param name="region">The Region Model to render. This object determines the View that will be used.</param>
        /// <param name="containerSize">The size (in grid column units) of the containing element.</param>
        /// <returns>The rendered HTML or an empty string if <paramref name="region"/> is <c>null</c>.</returns>
        public static MvcHtmlString DxaRegion(this HtmlHelper htmlHelper, RegionModel region, int containerSize = 0)
        {
            if (region == null)
            {
                return(MvcHtmlString.Empty);
            }

            if (containerSize == 0)
            {
                containerSize = SiteConfiguration.MediaHelper.GridSize;
            }

            using (new Tracer(htmlHelper, region, containerSize))
            {
                MvcData mvcData            = region.MvcData;
                string  actionName         = mvcData.ActionName ?? SiteConfiguration.GetRegionAction();
                string  controllerName     = mvcData.ControllerName ?? SiteConfiguration.GetRegionController();
                string  controllerAreaName = mvcData.ControllerAreaName ?? SiteConfiguration.GetDefaultModuleName();

                MvcHtmlString result = htmlHelper.Action(actionName, controllerName, new { Region = region, containerSize = containerSize, area = controllerAreaName });

                if (WebRequestContext.IsPreview)
                {
                    result = new MvcHtmlString(Markup.TransformXpmMarkupAttributes(result.ToString()));
                }
                return(Markup.DecorateMarkup(result, region));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new <see cref="MvcData"/> instance for a given qualified View name.
        /// </summary>
        /// <param name="qualifiedViewName">The qualified View name with format AreaName:ControllerName:ViewName.</param>
        public MvcData(string qualifiedViewName)
        {
            string[] qualifiedViewNameParts = qualifiedViewName.Split(':');
            switch (qualifiedViewNameParts.Length)
            {
            case 1:
                AreaName = SiteConfiguration.GetDefaultModuleName();
                ViewName = qualifiedViewNameParts[0];
                break;

            case 2:
                AreaName = qualifiedViewNameParts[0];
                ViewName = qualifiedViewNameParts[1];
                break;

            case 3:
                AreaName       = qualifiedViewNameParts[0];
                ControllerName = qualifiedViewNameParts[1];
                ViewName       = qualifiedViewNameParts[2];
                break;

            default:
                throw new DxaException(
                          string.Format("Invalid format for Qualified View Name: '{0}'. Format must be 'ViewName' or 'AreaName:ViewName' or 'AreaName:ControllerName:Vieweame.'",
                                        qualifiedViewName)
                          );
            }
        }
        /// <summary>
        /// Enriches a given Entity Model using an appropriate (custom) Controller.
        /// </summary>
        /// <param name="entity">The Entity Model to enrich.</param>
        /// <returns>The enriched Entity Model.</returns>
        /// <remarks>
        /// This method is different from <see cref="EnrichModel"/> in that it doesn't expect the current Controller to be able to enrich the Entity Model;
        /// it creates a Controller associated with the Entity Model for that purpose.
        /// It is used by <see cref="PageController.EnrichEmbeddedModels"/>.
        /// </remarks>
        protected EntityModel EnrichEntityModel(EntityModel entity)
        {
            if (entity == null || entity.MvcData == null || !IsCustomAction(entity.MvcData))
            {
                return(entity);
            }

            MvcData mvcData = entity.MvcData;

            using (new Tracer(entity, mvcData))
            {
                string controllerName     = mvcData.ControllerName ?? SiteConfiguration.GetEntityController();
                string controllerAreaName = mvcData.ControllerAreaName ?? SiteConfiguration.GetDefaultModuleName();

                RequestContext tempRequestContext = new RequestContext(HttpContext, new RouteData());
                tempRequestContext.RouteData.DataTokens["Area"]   = controllerAreaName;
                tempRequestContext.RouteData.Values["controller"] = controllerName;
                tempRequestContext.RouteData.Values["area"]       = controllerAreaName;

                // Note: Entity Controllers don't have to inherit from EntityController per se, but they must inherit from BaseController.
                BaseController entityController = (BaseController)ControllerBuilder.Current.GetControllerFactory().CreateController(tempRequestContext, controllerName);
                entityController.ControllerContext = new ControllerContext(HttpContext, tempRequestContext.RouteData, entityController);
                return((EntityModel)entityController.EnrichModel(entity));
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Renders a given Entity Model.
        /// </summary>
        /// <param name="htmlHelper">The HtmlHelper instance on which the extension method operates.</param>
        /// <param name="entity">The Entity to render.</param>
        /// <param name="containerSize">The size (in grid column units) of the containing element.</param>
        /// <returns>The rendered HTML or an empty string if <paramref name="entity"/> is <c>null</c>.</returns>
        public static MvcHtmlString DxaEntity(this HtmlHelper htmlHelper, EntityModel entity, int containerSize = 0)
        {
            if (entity == null)
            {
                return(MvcHtmlString.Empty);
            }

            if (containerSize == 0)
            {
                containerSize = SiteConfiguration.MediaHelper.GridSize;
            }

            MvcData mvcData = entity.MvcData;

            using (new Tracer(htmlHelper, entity, containerSize, mvcData))
            {
                if (mvcData == null)
                {
                    throw new DxaException($"Unable to render Entity Model [{entity}], because it has no MVC data.");
                }

                string actionName         = mvcData.ActionName ?? SiteConfiguration.GetEntityAction();
                string controllerName     = mvcData.ControllerName ?? SiteConfiguration.GetEntityController();
                string controllerAreaName = mvcData.ControllerAreaName ?? SiteConfiguration.GetDefaultModuleName();

                RouteValueDictionary parameters = new RouteValueDictionary();
                int parentContainerSize         = (int)htmlHelper.ViewData[DxaViewDataItems.ContainerSize];
                if (parentContainerSize == 0)
                {
                    parentContainerSize = SiteConfiguration.MediaHelper.GridSize;
                }
                parameters["containerSize"] = (containerSize * parentContainerSize) / SiteConfiguration.MediaHelper.GridSize;
                parameters["entity"]        = entity;
                parameters["area"]          = controllerAreaName;
                if (mvcData.RouteValues != null)
                {
                    foreach (string key in mvcData.RouteValues.Keys)
                    {
                        parameters[key] = mvcData.RouteValues[key];
                    }
                }

                MvcHtmlString result = htmlHelper.Action(actionName, controllerName, parameters);
                // If the Entity is being rendered inside a Region (typical), we don't have to transform the XPM markup attributes here; it will be done in DxaRegion.
                if (!(htmlHelper.ViewData.Model is RegionModel) && WebRequestContext.IsPreview)
                {
                    result = new MvcHtmlString(Markup.TransformXpmMarkupAttributes(result.ToString()));
                }
                return(Markup.DecorateMarkup(result, entity));
            }
        }
        protected virtual MvcData CreateMvcData(DataModel.MvcData data, string defaultControllerName)
        {
            if (data == null)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(data.ViewName))
            {
                throw new DxaException("No View Name specified in MVC Data.");
            }

            return(new MvcData
            {
                ControllerName = data.ControllerName ?? defaultControllerName,
                ControllerAreaName = data.ControllerAreaName ?? SiteConfiguration.GetDefaultModuleName(),
                ActionName = data.ActionName ?? defaultControllerName,
                ViewName = data.ViewName,
                AreaName = data.AreaName ?? SiteConfiguration.GetDefaultModuleName(),
                RouteValues = data.Parameters
            });
        }
 private static bool IsCustomAction(MvcData mvcData)
 {
     return(mvcData.ActionName != SiteConfiguration.GetEntityAction() ||
            mvcData.ControllerName != SiteConfiguration.GetEntityController() ||
            mvcData.ControllerAreaName != SiteConfiguration.GetDefaultModuleName());
 }