/// <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)); } }
/// <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)); } }
private static bool IsCustomAction(MvcData mvcData) { return(mvcData.ActionName != SiteConfiguration.GetEntityAction() || mvcData.ControllerName != SiteConfiguration.GetEntityController() || mvcData.ControllerAreaName != SiteConfiguration.GetDefaultModuleName()); }