Ejemplo n.º 1
0
        public string Execute(MacroModel macro, IPublishedContent content)
        {
            if (macro == null)
            {
                throw new ArgumentNullException("macro");
            }
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }
            if (macro.ScriptName.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("The ScriptName property of the macro object cannot be null or empty");
            }

            var http      = _getHttpContext();
            var umbCtx    = _getUmbracoContext();
            var routeVals = new RouteData();

            routeVals.Values.Add("controller", "PartialViewMacro");
            routeVals.Values.Add("action", "Index");
            routeVals.DataTokens.Add("umbraco-context", umbCtx); //required for UmbracoViewPage

            //lets render this controller as a child action if we are currently executing using MVC
            //(otherwise don't do this since we're using webforms)
            var mvcHandler  = http.CurrentHandler as MvcHandler;
            var viewContext = new ViewContext {
                ViewData = new ViewDataDictionary()
            };;

            if (mvcHandler != null)
            {
                //try and extract the current view context from the route values, this would be set in the UmbracoViewPage.
                if (mvcHandler.RequestContext.RouteData.DataTokens.ContainsKey(Umbraco.Web.Mvc.Constants.DataTokenCurrentViewContext))
                {
                    viewContext = (ViewContext)mvcHandler.RequestContext.RouteData.DataTokens[Umbraco.Web.Mvc.Constants.DataTokenCurrentViewContext];
                }
                routeVals.DataTokens.Add("ParentActionViewContext", viewContext);
            }

            var    request = new RequestContext(http, routeVals);
            string output;

            using (var controller = new PartialViewMacroController(macro, content))
            {
                //bubble up the model state from the main view context to our custom controller.
                //when merging we'll create a new dictionary, otherwise you might run into an enumeration error
                // caused from ModelStateDictionary
                controller.ModelState.Merge(new ModelStateDictionary(viewContext.ViewData.ModelState));
                controller.ControllerContext = new ControllerContext(request, controller);
                //call the action to render
                var result = controller.Index();
                output = controller.RenderViewResultAsString(result);
            }

            return(output);
        }
Ejemplo n.º 2
0
        public MacroContent Execute(MacroModel macro, IPublishedContent content)
        {
            if (macro == null)
            {
                throw new ArgumentNullException(nameof(macro));
            }
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }
            if (macro.MacroSource.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("The MacroSource property of the macro object cannot be null or empty");
            }

            var http      = _getHttpContext();
            var umbCtx    = _getUmbracoContext();
            var routeVals = new RouteData();

            routeVals.Values.Add("controller", "PartialViewMacro");
            routeVals.Values.Add("action", "Index");
            routeVals.DataTokens.Add(Core.Constants.Web.UmbracoContextDataToken, umbCtx); //required for UmbracoViewPage

            //lets render this controller as a child action
            var viewContext = new ViewContext {
                ViewData = new ViewDataDictionary()
            };

            //try and extract the current view context from the route values, this would be set in the UmbracoViewPage or in
            // the UmbracoPageResult if POSTing to an MVC controller but rendering in Webforms
            if (http.Request.RequestContext.RouteData.DataTokens.ContainsKey(Mvc.Constants.DataTokenCurrentViewContext))
            {
                viewContext = (ViewContext)http.Request.RequestContext.RouteData.DataTokens[Mvc.Constants.DataTokenCurrentViewContext];
            }
            routeVals.DataTokens.Add("ParentActionViewContext", viewContext);

            var    request = new RequestContext(http, routeVals);
            string output;

            using (var controller = new PartialViewMacroController(macro, content))
            {
                controller.ViewData = viewContext.ViewData;

                controller.ControllerContext = new ControllerContext(request, controller);

                //call the action to render
                var result = controller.Index();
                output = controller.RenderViewResultAsString(result);
            }

            return(new MacroContent {
                Text = output
            });
        }
Ejemplo n.º 3
0
        public string Execute(MacroModel macro, INode currentPage)
        {
            if (macro == null) throw new ArgumentNullException("macro");
            if (currentPage == null) throw new ArgumentNullException("currentPage");
			if (macro.ScriptName.IsNullOrWhiteSpace()) throw new ArgumentException("The ScriptName property of the macro object cannot be null or empty");
		
            if (!macro.ScriptName.StartsWith(SystemDirectories.MvcViews + "/MacroPartials/")
                && (!Regex.IsMatch(macro.ScriptName, "~/App_Plugins/.+?/Views/MacroPartials", RegexOptions.Compiled)))
            {
                throw new InvalidOperationException("Cannot render the Partial View Macro with file: " + macro.ScriptName + ". All Partial View Macros must exist in the " + SystemDirectories.MvcViews + "/MacroPartials/ folder");
            }

            var http = _getHttpContext();
            var umbCtx = _getUmbracoContext();
            var routeVals = new RouteData();
            routeVals.Values.Add("controller", "PartialViewMacro");
            routeVals.Values.Add("action", "Index");
            routeVals.DataTokens.Add("umbraco-context", umbCtx); //required for UmbracoViewPage

			//lets render this controller as a child action if we are currently executing using MVC 
			//(otherwise don't do this since we're using webforms)
			var mvcHandler = http.CurrentHandler as MvcHandler;
			var viewContext = new ViewContext {ViewData = new ViewDataDictionary()};;
			if (mvcHandler != null)
			{
				//try and extract the current view context from the route values, this would be set in the UmbracoViewPage.
				if (mvcHandler.RequestContext.RouteData.DataTokens.ContainsKey(Constants.DataTokenCurrentViewContext))
				{
					viewContext = (ViewContext) mvcHandler.RequestContext.RouteData.DataTokens[Constants.DataTokenCurrentViewContext];
				}
				routeVals.DataTokens.Add("ParentActionViewContext", viewContext);
			}

            var request = new RequestContext(http, routeVals);
            string output;
            using (var controller = new PartialViewMacroController(umbCtx, macro, currentPage))
            {
				//bubble up the model state from the main view context to our custom controller.
				//when merging we'll create a new dictionary, otherwise you might run into an enumeration error
				// caused from ModelStateDictionary
				controller.ModelState.Merge(new ModelStateDictionary(viewContext.ViewData.ModelState));
				controller.ControllerContext = new ControllerContext(request, controller);
				//call the action to render
                var result = controller.Index();
				output = controller.RenderViewResultAsString(result);
            }

            return output;
        }
        public string Execute(MacroModel macro, IPublishedContent content)
        {
            if (macro == null) throw new ArgumentNullException("macro");
            if (content == null) throw new ArgumentNullException("content");
			if (macro.ScriptName.IsNullOrWhiteSpace()) throw new ArgumentException("The ScriptName property of the macro object cannot be null or empty");
		
            var http = _getHttpContext();
            var umbCtx = _getUmbracoContext();
            var routeVals = new RouteData();
            routeVals.Values.Add("controller", "PartialViewMacro");
            routeVals.Values.Add("action", "Index");
            routeVals.DataTokens.Add("umbraco-context", umbCtx); //required for UmbracoViewPage

			//lets render this controller as a child action if we are currently executing using MVC 
			//(otherwise don't do this since we're using webforms)
			var mvcHandler = http.CurrentHandler as MvcHandler;
			var viewContext = new ViewContext {ViewData = new ViewDataDictionary()};;
			if (mvcHandler != null)
			{
				//try and extract the current view context from the route values, this would be set in the UmbracoViewPage.
				if (mvcHandler.RequestContext.RouteData.DataTokens.ContainsKey(Umbraco.Web.Mvc.Constants.DataTokenCurrentViewContext))
				{
					viewContext = (ViewContext) mvcHandler.RequestContext.RouteData.DataTokens[Umbraco.Web.Mvc.Constants.DataTokenCurrentViewContext];
				}
				routeVals.DataTokens.Add("ParentActionViewContext", viewContext);
			}

            var request = new RequestContext(http, routeVals);
            string output;
            using (var controller = new PartialViewMacroController(macro, content))
            {
				//bubble up the model state from the main view context to our custom controller.
				//when merging we'll create a new dictionary, otherwise you might run into an enumeration error
				// caused from ModelStateDictionary
				controller.ModelState.Merge(new ModelStateDictionary(viewContext.ViewData.ModelState));
				controller.ControllerContext = new ControllerContext(request, controller);
				//call the action to render
                var result = controller.Index();
				output = controller.RenderViewResultAsString(result);
            }

            return output;
        }
Ejemplo n.º 5
0
        public string Execute(MacroModel macro, INode currentPage)
        {
            if (macro == null)
            {
                throw new ArgumentNullException("macro");
            }
            if (currentPage == null)
            {
                throw new ArgumentNullException("currentPage");
            }
            if (macro.ScriptName.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("The ScriptName property of the macro object cannot be null or empty");
            }

            if (!macro.ScriptName.StartsWith(SystemDirectories.MvcViews + "/MacroPartials/") &&
                (!Regex.IsMatch(macro.ScriptName, "~/App_Plugins/.+?/Views/MacroPartials", RegexOptions.Compiled)))
            {
                throw new InvalidOperationException("Cannot render the Partial View Macro with file: " + macro.ScriptName + ". All Partial View Macros must exist in the " + SystemDirectories.MvcViews + "/MacroPartials/ folder");
            }

            var http      = _getHttpContext();
            var umbCtx    = _getUmbracoContext();
            var routeVals = new RouteData();

            routeVals.Values.Add("controller", "PartialViewMacro");
            routeVals.Values.Add("action", "Index");
            routeVals.DataTokens.Add("umbraco-context", umbCtx); //required for UmbracoViewPage

            //lets render this controller as a child action if we are currently executing using MVC
            //(otherwise don't do this since we're using webforms)
            var mvcHandler  = http.CurrentHandler as MvcHandler;
            var viewContext = new ViewContext {
                ViewData = new ViewDataDictionary()
            };;

            if (mvcHandler != null)
            {
                //try and extract the current view context from the route values, this would be set in the UmbracoViewPage.
                if (mvcHandler.RequestContext.RouteData.DataTokens.ContainsKey(Constants.DataTokenCurrentViewContext))
                {
                    viewContext = (ViewContext)mvcHandler.RequestContext.RouteData.DataTokens[Constants.DataTokenCurrentViewContext];
                }
                routeVals.DataTokens.Add("ParentActionViewContext", viewContext);
            }

            var    request = new RequestContext(http, routeVals);
            string output;

            using (var controller = new PartialViewMacroController(umbCtx, macro, currentPage))
            {
                //bubble up the model state from the main view context to our custom controller.
                //when merging we'll create a new dictionary, otherwise you might run into an enumeration error
                // caused from ModelStateDictionary
                controller.ModelState.Merge(new ModelStateDictionary(viewContext.ViewData.ModelState));
                controller.ControllerContext = new ControllerContext(request, controller);
                //call the action to render
                var result = controller.Index();
                output = controller.RenderViewResultAsString(result);
            }

            return(output);
        }