/// <summary> /// Returns a RouteDefinition object based on the current renderModel /// </summary> /// <param name="requestContext"></param> /// <param name="renderModel"></param> /// <returns></returns> protected virtual RouteDefinition GetUmbracoRouteDefinition(RequestContext requestContext, IUmbracoRenderModel renderModel) { //creates the default route definition which maps to the 'UmbracoController' controller var def = new RouteDefinition { ControllerName = UmbracoController.GetControllerName<UmbracoController>(), Controller = new UmbracoController(), RenderModel = renderModel, ActionName = ((Route)requestContext.RouteData.Route).Defaults["action"].ToString() }; //check that a template is defined) if (renderModel.CurrentNode != null && renderModel.CurrentNode.CurrentTemplate != null) { using (var uow = _applicationContext.Hive.OpenReader<IFileStore>(new Uri("storage://templates"))) { var template = uow.Repositories.Get<File>(renderModel.CurrentNode.CurrentTemplate.Id); //check if there's a custom controller assigned, base on the document type alias. var controller = _controllerFactory.CreateController(requestContext, renderModel.CurrentNode.ContentType.Alias); //check if that controller exists if (controller != null) { //ensure the controller is of type 'UmbracoController' if (controller is UmbracoController) { //set the controller and name to the custom one def.Controller = (ControllerBase)controller; def.ControllerName = UmbracoController.GetControllerName(controller.GetType()); } else { LogHelper.Warn<RenderRouteHandler>("The current Document Type {0} matches a locally declared controller of type {1}. Custom Controllers for Umbraco routing must inherit from '{2}'.", renderModel.CurrentNode.ContentType.Alias, controller.GetType().FullName, typeof(UmbracoController).FullName); //exit as we cannnot route to the custom controller, just route to the standard one. return def; } // Template might be null if none is assigned to the node if (template != null) { //check if the custom controller has an action with the same name as the template name (we convert ToUmbracoAlias since the template name might have invalid chars). //NOTE: This also means that all custom actions MUST be PascalCase.. but that should be standard. var templateName = template.Name.Split('.')[0].ToUmbracoAlias(StringAliasCaseType.PascalCase); var action = controller.GetType().GetMethod(templateName); if (action != null) { def.ActionName = templateName; } //otherwise it will continue to route to Index since thats teh default route } } } } return def; }
/// <summary> /// Handles a posted form to an Umbraco Url and ensures the correct controller is routed to and that /// the right DataTokens are set. /// </summary> /// <param name="requestContext"></param> /// <param name="postedInfo"></param> /// <param name="renderModel"></param> /// <param name="routeDefinition">The original route definition that would normally be used to route if it were not a POST</param> protected virtual IHttpHandler HandlePostedValues(RequestContext requestContext, PostedDataProxyInfo postedInfo, IUmbracoRenderModel renderModel, RouteDefinition routeDefinition) { //set the standard route values/tokens requestContext.RouteData.Values["controller"] = postedInfo.ControllerName; requestContext.RouteData.Values["action"] = postedInfo.ActionName; requestContext.RouteData.DataTokens["area"] = postedInfo.Area; IHttpHandler handler = new MvcHandler(requestContext); //ensure the surface id is set if found, meaning it is a plugin, not locally declared if (postedInfo.SurfaceId != default(Guid)) { requestContext.RouteData.Values["surfaceId"] = postedInfo.SurfaceId.ToString("N"); //find the other data tokens for this route and merge... things like Namespace will be included here using (RouteTable.Routes.GetReadLock()) { var surfaceRoute = RouteTable.Routes.OfType <Route>() .Where(x => x.Defaults != null && x.Defaults.ContainsKey("surfaceId") && x.Defaults["surfaceId"].ToString() == postedInfo.SurfaceId.ToString("N")) .SingleOrDefault(); if (surfaceRoute == null) { throw new InvalidOperationException("Could not find a Surface controller route in the RouteTable for id " + postedInfo.SurfaceId); } //set the 'Namespaces' token so the controller factory knows where to look to construct it if (surfaceRoute.DataTokens.ContainsKey("Namespaces")) { requestContext.RouteData.DataTokens["Namespaces"] = surfaceRoute.DataTokens["Namespaces"]; } handler = surfaceRoute.RouteHandler.GetHttpHandler(requestContext); } } //store the original URL this came in on requestContext.RouteData.DataTokens["umbraco-item-url"] = requestContext.HttpContext.Request.Url.AbsolutePath; //store the original route definition requestContext.RouteData.DataTokens["umbraco-route-def"] = routeDefinition; return(handler); }
/// <summary> /// Handles a posted form to an Umbraco Url and ensures the correct controller is routed to and that /// the right DataTokens are set. /// </summary> /// <param name="requestContext"></param> /// <param name="postedInfo"></param> /// <param name="renderModel"></param> /// <param name="routeDefinition">The original route definition that would normally be used to route if it were not a POST</param> protected virtual IHttpHandler HandlePostedValues(RequestContext requestContext, PostedDataProxyInfo postedInfo, IUmbracoRenderModel renderModel, RouteDefinition routeDefinition) { //set the standard route values/tokens requestContext.RouteData.Values["controller"] = postedInfo.ControllerName; requestContext.RouteData.Values["action"] = postedInfo.ActionName; requestContext.RouteData.DataTokens["area"] = postedInfo.Area; IHttpHandler handler = new MvcHandler(requestContext); //ensure the surface id is set if found, meaning it is a plugin, not locally declared if (postedInfo.SurfaceId != default(Guid)) { requestContext.RouteData.Values["surfaceId"] = postedInfo.SurfaceId.ToString("N"); //find the other data tokens for this route and merge... things like Namespace will be included here using (RouteTable.Routes.GetReadLock()) { var surfaceRoute = RouteTable.Routes.OfType<Route>() .Where(x => x.Defaults != null && x.Defaults.ContainsKey("surfaceId") && x.Defaults["surfaceId"].ToString() == postedInfo.SurfaceId.ToString("N")) .SingleOrDefault(); if (surfaceRoute == null) throw new InvalidOperationException("Could not find a Surface controller route in the RouteTable for id " + postedInfo.SurfaceId); //set the 'Namespaces' token so the controller factory knows where to look to construct it if (surfaceRoute.DataTokens.ContainsKey("Namespaces")) { requestContext.RouteData.DataTokens["Namespaces"] = surfaceRoute.DataTokens["Namespaces"]; } handler = surfaceRoute.RouteHandler.GetHttpHandler(requestContext); } } //store the original URL this came in on requestContext.RouteData.DataTokens["umbraco-item-url"] = requestContext.HttpContext.Request.Url.AbsolutePath; //store the original route definition requestContext.RouteData.DataTokens["umbraco-route-def"] = routeDefinition; return handler; }