コード例 #1
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var umbracoContext = GetUmbracoContext(requestContext);

            var found = FindContent(requestContext, umbracoContext);

            if (found == null)
            {
                return(new NotFoundHandler());
            }

            umbracoContext.PublishedContentRequest = new PublishedContentRequest(
                umbracoContext.CleanedUmbracoUrl, umbracoContext.RoutingContext,
                UmbracoConfig.For.UmbracoSettings().WebRouting, s => Roles.Provider.GetRolesForUser(s))
            {
                PublishedContent = found
            };

            //allows inheritors to change the pcr
            PreparePublishedContentRequest(umbracoContext.PublishedContentRequest);

            //create the render model
            var renderModel = new RenderModel(umbracoContext.PublishedContentRequest.PublishedContent, umbracoContext.PublishedContentRequest.Culture);

            //assigns the required tokens to the request
            requestContext.RouteData.DataTokens.Add(Core.Constants.Web.UmbracoDataToken, renderModel);
            requestContext.RouteData.DataTokens.Add(Core.Constants.Web.PublishedDocumentRequestDataToken, umbracoContext.PublishedContentRequest);
            requestContext.RouteData.DataTokens.Add(Core.Constants.Web.UmbracoContextDataToken, umbracoContext);
            //this is used just for a flag that this is an umbraco custom route
            requestContext.RouteData.DataTokens.Add(Core.Constants.Web.CustomRouteDataToken, true);

            //Here we need to detect if a SurfaceController has posted
            var formInfo = RenderRouteHandler.GetFormInfo(requestContext);

            if (formInfo != null)
            {
                var def = new RouteDefinition
                {
                    ActionName              = requestContext.RouteData.GetRequiredString("action"),
                    ControllerName          = requestContext.RouteData.GetRequiredString("controller"),
                    PublishedContentRequest = umbracoContext.PublishedContentRequest
                };

                //set the special data token to the current route definition
                requestContext.RouteData.DataTokens[Umbraco.Core.Constants.Web.UmbracoRouteDefinitionDataToken] = def;

                return(RenderRouteHandler.HandlePostedValues(requestContext, formInfo));
            }

            return(new MvcHandler(requestContext));
        }
コード例 #2
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var umbracoContext = UmbracoContext.Current;

            var found = FindContent(requestContext, umbracoContext);

            if (found == null)
            {
                return(new NotFoundHandler());
            }

            umbracoContext.PublishedContentRequest = new PublishedContentRequest(
                umbracoContext.CleanedUmbracoUrl, umbracoContext.RoutingContext)
            {
                PublishedContent = found
            };

            //allows inheritors to change the pcr
            PreparePublishedContentRequest(umbracoContext.PublishedContentRequest);

            //create the render model
            var renderModel = new RenderModel(umbracoContext.PublishedContentRequest.PublishedContent, umbracoContext.PublishedContentRequest.Culture);

            //assigns the required tokens to the request
            requestContext.RouteData.DataTokens.Add("umbraco", renderModel);
            requestContext.RouteData.DataTokens.Add("umbraco-doc-request", umbracoContext.PublishedContentRequest);
            requestContext.RouteData.DataTokens.Add("umbraco-context", umbracoContext);
            //this is used just for a flag that this is an umbraco custom route
            requestContext.RouteData.DataTokens.Add("umbraco-custom-route", true);

            //Here we need to detect if a SurfaceController has posted
            var formInfo = RenderRouteHandler.GetFormInfo(requestContext);

            if (formInfo != null)
            {
                var def = new RouteDefinition
                {
                    ActionName              = requestContext.RouteData.GetRequiredString("action"),
                    ControllerName          = requestContext.RouteData.GetRequiredString("controller"),
                    PublishedContentRequest = umbracoContext.PublishedContentRequest
                };

                //set the special data token to the current route definition
                requestContext.RouteData.DataTokens["umbraco-route-def"] = def;

                return(RenderRouteHandler.HandlePostedValues(requestContext, formInfo));
            }

            return(new MvcHandler(requestContext));
        }
コード例 #3
0
		public void Umbraco_Route_User_Defined_Controller_Action(string templateName)
		{
			var template = Template.MakeNew(templateName, new User(0));
			var route = RouteTable.Routes["Umbraco_default"];
			var routeData = new RouteData() {Route = route};
			var routingContext = GetRoutingContext("~/dummy-page", template, routeData);
			var docRequest = new PublishedContentRequest(routingContext.UmbracoContext.CleanedUmbracoUrl, routingContext)
				{
					PublishedContent = routingContext.PublishedContentStore.GetDocumentById(routingContext.UmbracoContext, 1172), 
					Template = template
				};

			var handler = new RenderRouteHandler(new TestControllerFactory(), routingContext.UmbracoContext);

			handler.GetHandlerForRoute(routingContext.UmbracoContext.HttpContext.Request.RequestContext, docRequest);
			Assert.AreEqual("CustomDocument", routeData.Values["controller"].ToString());
			Assert.AreEqual("HomePage", routeData.Values["action"].ToString());
		}
コード例 #4
0
		public void Umbraco_Route_Umbraco_Defined_Controller_Action()
		{
            var template = CreateTemplate("homePage");
			var route = RouteTable.Routes["Umbraco_default"];
			var routeData = new RouteData() { Route = route };
			var routingContext = GetRoutingContext("~/dummy-page", template.Id, routeData);
			var docRequest = new PublishedContentRequest(routingContext.UmbracoContext.CleanedUmbracoUrl, routingContext)
			{
                PublishedContent = routingContext.UmbracoContext.ContentCache.GetById(1174),
				TemplateModel = template
			};

			var handler = new RenderRouteHandler(new TestControllerFactory(), routingContext.UmbracoContext);

			handler.GetHandlerForRoute(routingContext.UmbracoContext.HttpContext.Request.RequestContext, docRequest);
			Assert.AreEqual("RenderMvc", routeData.Values["controller"].ToString());
            //the route action will still be the one we've asked for because our RenderActionInvoker is the thing that decides
            // if the action matches.
            Assert.AreEqual("homePage", routeData.Values["action"].ToString());
		}
コード例 #5
0
        public override void ExecuteResult(ControllerContext context)
        {
            ResetRouteData(context.RouteData);

            ValidateRouteData(context.RouteData);

            var routeDef = (RouteDefinition)context.RouteData.DataTokens[Umbraco.Core.Constants.Web.UmbracoRouteDefinitionDataToken];

            //Special case, if it is webforms but we're posting to an MVC surface controller, then we
            // need to return the webforms result instead
            if (routeDef.PublishedContentRequest.RenderingEngine == RenderingEngine.WebForms)
            {
                EnsureViewContextForWebForms(context);
                var webFormsHandler = RenderRouteHandler.GetWebFormsHandler();
                webFormsHandler.ProcessRequest(HttpContext.Current);
            }
            else
            {
                var factory = ControllerBuilder.Current.GetControllerFactory();
                context.RouteData.Values["action"] = routeDef.ActionName;
                ControllerBase controller = null;

                try
                {
                    controller = CreateController(context, factory, routeDef);

                    CopyControllerData(context, controller);

                    ExecuteControllerAction(context, controller);
                }
                finally
                {
                    CleanupController(controller, factory);
                }
            }
        }
コード例 #6
0
		private void ExecuteTemplateRendering(TextWriter sw, PublishedContentRequest contentRequest)
		{
			//NOTE: Before we used to build up the query strings here but this is not necessary because when we do a 
			// Server.Execute in the TemplateRenderer, we pass in a 'true' to 'preserveForm' which automatically preserves all current
			// query strings so there's no need for this. HOWEVER, once we get MVC involved, we might have to do some fun things,
			// though this will happen in the TemplateRenderer.

			//var queryString = _umbracoContext.HttpContext.Request.QueryString.AllKeys
			//	.ToDictionary(key => key, key => context.Request.QueryString[key]);
			
			switch (contentRequest.RenderingEngine)
			{
				case RenderingEngine.Mvc:
					var requestContext = new RequestContext(_umbracoContext.HttpContext, new RouteData()
					{
						Route = RouteTable.Routes["Umbraco_default"]
					});
					var routeHandler = new RenderRouteHandler(ControllerBuilder.Current.GetControllerFactory(), _umbracoContext);
					var routeDef = routeHandler.GetUmbracoRouteDefinition(requestContext, contentRequest);
					var renderModel = new RenderModel(contentRequest.PublishedContent, contentRequest.Culture);
					//manually add the action/controller, this is required by mvc
					requestContext.RouteData.Values.Add("action", routeDef.ActionName);
					requestContext.RouteData.Values.Add("controller", routeDef.ControllerName);
					//add the rest of the required route data
					routeHandler.SetupRouteDataForRequest(renderModel, requestContext, contentRequest);
					//create and assign the controller context
					routeDef.Controller.ControllerContext = new ControllerContext(requestContext, routeDef.Controller);
					//render as string
					var stringOutput = routeDef.Controller.RenderViewToString(
						routeDef.ActionName,
						renderModel);
					sw.Write(stringOutput);
					break;
				case RenderingEngine.WebForms:
				default:
					var webFormshandler = (global::umbraco.UmbracoDefault)BuildManager
						.CreateInstanceFromVirtualPath("~/default.aspx", typeof(global::umbraco.UmbracoDefault));
					//the 'true' parameter will ensure that the current query strings are carried through, we don't have
					// to build up the url again, it will just work.
					_umbracoContext.HttpContext.Server.Execute(webFormshandler, sw, true);
					break;
			}	
			
		}
コード例 #7
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var umbracoContext = UmbracoContext.Current;

            //TODO: This is a huge hack - we need to publicize some stuff in the core
            //TODO: Instead of using this we can use the native route handlers in umbraco 7.2+
            var ensurePcr = new EnsurePublishedContentRequestAttribute(umbracoContext, "__virtualnodefinder__");

            var found = FindContent(requestContext, umbracoContext);
            if (found == null) return new NotFoundHandler();

            //assign the node to our special token
            requestContext.RouteData.DataTokens["__virtualnodefinder__"] = found;

            //this hack creates and assigns the pcr to the context - from 7.2+ this also calls Prepare() to 
            // wire up everything in the request
            ensurePcr.OnActionExecuted(new ActionExecutedContext { RequestContext = requestContext });

            //allows inheritors to change the pcr - obsolete though!
            PreparePublishedContentRequest(umbracoContext.PublishedContentRequest);

            //This doesn't execute for less than 7.2.0
            if (UmbracoVersion.Current < Version.Parse("7.2.0"))
            {
                umbracoContext.PublishedContentRequest.ConfigureRequest();
            }

            //create the render model
            var renderModel = new RenderModel(umbracoContext.PublishedContentRequest.PublishedContent, umbracoContext.PublishedContentRequest.Culture);

            //assigns the required tokens to the request
            requestContext.RouteData.DataTokens.Add("umbraco", renderModel);
            requestContext.RouteData.DataTokens.Add("umbraco-doc-request", umbracoContext.PublishedContentRequest);
            requestContext.RouteData.DataTokens.Add("umbraco-context", umbracoContext);

            //Here we need to detect if a SurfaceController has posted
            var formInfo = GetFormInfo(requestContext);
            if (formInfo != null)
            {
                //TODO: We are using reflection for this but with the issue http://issues.umbraco.org/issue/U4-5710 fixed we 
                // probably won't need to use our own custom router

                //in order to allow a SurfaceController to work properly, the correct data token needs to be set, so we need to 
                // add a custom RouteDefinition to the collection
                var handle = Activator.CreateInstance("umbraco", "Umbraco.Web.Mvc.RouteDefinition", false, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, null, null, null);
                var def = handle.Unwrap();

                def.SetPropertyValue("PublishedContentRequest", umbracoContext.PublishedContentRequest);
                def.SetPropertyValue("ControllerName", requestContext.RouteData.GetRequiredString("controller"));
                def.SetPropertyValue("ActionName", requestContext.RouteData.GetRequiredString("action"));

                requestContext.RouteData.DataTokens["umbraco-route-def"] = def;

                try
                {
                    //First try to call this method as a static method (since it is a static method in umbraco 7.2)
                    // if that fails then we will call it with a non static instance since that is how it was pre-7.2)
                    return (IHttpHandler)typeof(RenderRouteHandler).CallStaticMethod("HandlePostedValues", requestContext, (object)formInfo);
                }
                catch (TargetException)
                {
                    var rrh = new RenderRouteHandler(ControllerBuilder.Current.GetControllerFactory());
                    return (IHttpHandler)rrh.CallMethod("HandlePostedValues", requestContext, (object)formInfo);
                }
            }

            return new MvcHandler(requestContext);
        }
コード例 #8
0
        public void Umbraco_Route_User_Defined_Controller_Action(string templateName)
		{
            // NOTE - here we create templates with crazy aliases... assuming that these
            // could exist in the database... yet creating templates should sanitize
            // aliases one way or another...

            var template = CreateTemplate(templateName);
            var route = RouteTable.Routes["Umbraco_default"];
			var routeData = new RouteData() {Route = route};
			var routingContext = GetRoutingContext("~/dummy-page", template.Id, routeData, true);
			var docRequest = new PublishedContentRequest(routingContext.UmbracoContext.CleanedUmbracoUrl, routingContext)
				{
                    PublishedContent = routingContext.UmbracoContext.ContentCache.GetById(1172), 
					TemplateModel = template
				};

			var handler = new RenderRouteHandler(new TestControllerFactory(), routingContext.UmbracoContext);

			handler.GetHandlerForRoute(routingContext.UmbracoContext.HttpContext.Request.RequestContext, docRequest);
			Assert.AreEqual("CustomDocument", routeData.Values["controller"].ToString());
		    Assert.AreEqual(
                //global::umbraco.cms.helpers.Casing.SafeAlias(template.Alias),
                template.Alias.ToSafeAlias(),
		        routeData.Values["action"].ToString());
		}