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: publicize: ctor (or static method to create it), Prepared()
            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
            ensurePcr.OnActionExecuted(new ActionExecutedContext {
                RequestContext = requestContext
            });

            //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);

            umbracoContext.PublishedContentRequest.ConfigureRequest();

            return(new MvcHandler(requestContext));
        }
Exemple #2
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);
        }