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()); }
private void ExecuteTemplateRendering(TextWriter sw, PublishedRequest request) { //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]); var requestContext = new RequestContext(_umbracoContextAccessor.UmbracoContext.HttpContext, new RouteData() { Route = RouteTable.Routes["Umbraco_default"] }); var routeHandler = new RenderRouteHandler(_umbracoContextAccessor, ControllerBuilder.Current.GetControllerFactory()); var routeDef = routeHandler.GetUmbracoRouteDefinition(requestContext, request); var renderModel = new ContentModel(request.PublishedContent); //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, request); var stringOutput = RenderUmbracoRequestToString(requestContext); sw.Write(stringOutput); }
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 umbracoContext = GetUmbracoContext("~/dummy-page", template.Id, routeData, true); var publishedRouter = CreatePublishedRouter(); var frequest = publishedRouter.CreateRequest(umbracoContext); frequest.PublishedContent = umbracoContext.ContentCache.GetById(1172); frequest.TemplateModel = template; var type = new AutoPublishedContentType(22, "CustomDocument", new PublishedPropertyType[] { }); ContentTypesCache.GetPublishedContentTypeByAlias = alias => type; var handler = new RenderRouteHandler(umbracoContext, new TestControllerFactory(umbracoContext, Mock.Of <ILogger>())); handler.GetHandlerForRoute(umbracoContext.HttpContext.Request.RequestContext, frequest); Assert.AreEqual("CustomDocument", routeData.Values["controller"].ToString()); Assert.AreEqual( //global::umbraco.cms.helpers.Casing.SafeAlias(template.Alias), template.Alias.ToSafeAlias(), routeData.Values["action"].ToString()); }
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, RenderingEngine = RenderingEngine.Mvc }; var handler = new RenderRouteHandler( new TestControllerFactory(routingContext.UmbracoContext, Mock.Of <ILogger>()), 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()); }
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; } }
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 umbracoContext = GetUmbracoContext("~/dummy-page", template.Id, routeData, true); var publishedRouter = CreatePublishedRouter(); var frequest = publishedRouter.CreateRequest(umbracoContext); frequest.PublishedContent = umbracoContext.ContentCache.GetById(1172); frequest.TemplateModel = template; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); var type = new AutoPublishedContentType(22, "CustomDocument", new PublishedPropertyType[] { }); ContentTypesCache.GetPublishedContentTypeByAlias = alias => type; var handler = new RenderRouteHandler(umbracoContext, new TestControllerFactory(umbracoContextAccessor, Mock.Of <ILogger>(), context => { var membershipHelper = new MembershipHelper( umbracoContext.HttpContext, Mock.Of <IPublishedMemberCache>(), Mock.Of <MembershipProvider>(), Mock.Of <RoleProvider>(), Mock.Of <IMemberService>(), Mock.Of <IMemberTypeService>(), Mock.Of <IUserService>(), Mock.Of <IPublicAccessService>(), Mock.Of <AppCaches>(), Mock.Of <ILogger>()); return(new CustomDocumentController(Factory.GetInstance <IGlobalSettings>(), umbracoContextAccessor, Factory.GetInstance <ServiceContext>(), Factory.GetInstance <AppCaches>(), Factory.GetInstance <IProfilingLogger>(), new UmbracoHelper(Mock.Of <IPublishedContent>(), Mock.Of <ITagQuery>(), Mock.Of <ICultureDictionaryFactory>(), Mock.Of <IUmbracoComponentRenderer>(), Mock.Of <IPublishedContentQuery>(), membershipHelper))); })); handler.GetHandlerForRoute(umbracoContext.HttpContext.Request.RequestContext, frequest); Assert.AreEqual("CustomDocument", routeData.Values["controller"].ToString()); Assert.AreEqual( //global::umbraco.cms.helpers.Casing.SafeAlias(template.Alias), template.Alias.ToSafeAlias(), routeData.Values["action"].ToString()); }
public void Umbraco_Route_Umbraco_Defined_Controller_Action() { var template = Template.MakeNew("homePage", 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, 1174), Template = template }; var handler = new RenderRouteHandler(new TestControllerFactory(), routingContext.UmbracoContext); handler.GetHandlerForRoute(routingContext.UmbracoContext.HttpContext.Request.RequestContext, docRequest); Assert.AreEqual("RenderMvc", routeData.Values["controller"].ToString()); Assert.AreEqual("Index", routeData.Values["action"].ToString()); }
public void InitTest() { Init(); RenderModelFactory = FakeRenderModelFactory.CreateWithApp(); var frontEndRouteHandler = new RenderRouteHandler(new TestControllerFactory(), UmbracoApplicationContext, RenderModelFactory); //register areas/routes... RouteTable.Routes.Clear(); var packageFolder = new DirectoryInfo(Path.Combine(Common.CurrentAssemblyDirectory, "App_Plugins", PluginManager.PackagesFolderName, "TestPackage")); Components = new ComponentRegistrations(new List <Lazy <MenuItem, MenuItemMetadata> >(), GetEditorMetadata(packageFolder), GetTreeMetadata(packageFolder), GetSurfaceMetadata(packageFolder), new List <Lazy <AbstractTask, TaskMetadata> >(), new List <Lazy <PropertyEditor, PropertyEditorMetadata> >(), new List <Lazy <AbstractParameterEditor, ParameterEditorMetadata> >(), new List <Lazy <DashboardMatchRule, DashboardRuleMetadata> >(), new List <Lazy <DashboardFilter, DashboardRuleMetadata> >(), new List <Lazy <Permission, PermissionMetadata> >(), new List <Lazy <AbstractMacroEngine, MacroEngineMetadata> >()); var componentRegistration = new PackageAreaRegistration(packageFolder, UmbracoApplicationContext, Components); var areaRegistration = new UmbracoAreaRegistration(UmbracoApplicationContext, Components); var installRegistration = new InstallAreaRegistration(UmbracoApplicationContext.Settings); var cmsBootstrapper = new CmsBootstrapper(UmbracoApplicationContext.Settings, areaRegistration, installRegistration, new[] { componentRegistration }, new DefaultAttributeTypeRegistry()); var renderBootstrapper = new RenderBootstrapper(UmbracoApplicationContext, frontEndRouteHandler, RenderModelFactory); //bootstrappers setup the routes cmsBootstrapper.Boot(RouteTable.Routes); renderBootstrapper.Boot(RouteTable.Routes); new UmbracoWebApplication(null, null, null).RegisterRoutes(RouteTable.Routes); }
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 umbracoContext = GetUmbracoContext("~/dummy-page", template.Id, routeData); var publishedRouter = CreatePublishedRouter(); var frequest = publishedRouter.CreateRequest(umbracoContext); frequest.PublishedContent = umbracoContext.ContentCache.GetById(1174); frequest.TemplateModel = template; var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); var handler = new RenderRouteHandler(umbracoContext, new TestControllerFactory(umbracoContextAccessor, Mock.Of <ILogger>())); handler.GetHandlerForRoute(umbracoContext.HttpContext.Request.RequestContext, frequest); 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()); }
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( global::umbraco.cms.helpers.Casing.SafeAlias(templateName), routeData.Values["action"].ToString()); }
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); }